Can a C macro definition refer to other macros?

What I'm trying to figure out is if something such as this (written in C):

#define FOO 15
#define BAR 23
#define MEH (FOO / BAR)

is allowed? I would want the preprocessor to replace every instance of

MEH

with

(15 / 23)

but I'm not so sure that will work. Certainly if the preprocessor only goes through the code once then I don't think it'd work out the way I'd like.

I found several similar examples but all were really too complicated for me to understand. If someone could help me out with this simple one I'd be eternally grateful!


Short answer yes. You can nest defines and macros like that - as many levels as you want as long as it isn't recursive.


The answer is "yes", and two other people have correctly said so.

As for why the answer is yes, the gory details are in the C standard, section 6.10.3.4, "Rescanning and further replacement". The OP might not benefit from this, but others might be interested.

6.10.3.4 Rescanning and further replacement

After all parameters in the replacement list have been substituted and # and ## processing has taken place, all placemarker preprocessing tokens are removed. Then, the resulting preprocessing token sequence is rescanned, along with all subsequent preprocessing tokens of the source file, for more macro names to replace.

If the name of the macro being replaced is found during this scan of the replacement list (not including the rest of the source file's preprocessing tokens), it is not replaced. Furthermore, if any nested replacements encounter the name of the macro being replaced, it is not replaced. These nonreplaced macro name preprocessing tokens are no longer available for further replacement even if they are later (re)examined in contexts in which that macro name preprocessing token would otherwise have been replaced.

The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles one, but all pragma unary operator expressions within it are then processed as specified in 6.10.9 below.