Can a C macro definition refer to other macros?

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.


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


Yes, it's going to work.


But for your personal information, here are some simplified rules about macros that might help you (it's out of scope, but will probably help you in the future). I'll try to keep it as simple as possible.

  • The defines are "defined" in the order they are included/read. That means that you cannot use a define that wasn't defined previously.

  • Usefull pre-processor keyword: #define, #undef, #else, #elif, #ifdef, #ifndef, #if

  • You can use any other previously #define in your macro. They will be expanded. (like in your question)

  • Function macro definitions accept two special operators (# and ##)

operator # stringize the argument:

#define str(x) #x
str(test); // would translate to "test"

operator ## concatenates two arguments

#define concat(a,b) a ## b
concat(hello, world); // would translate to "helloworld"

There are some predefined macros (from the language) as well that you can use:

__LINE__, __FILE__, __cplusplus, etc

See your compiler section on that to have an extensive list since it's not "cross platform"

  • Pay attention to the macro expansion

You'll see that people uses a log of round brackets "()" when defining macros. The reason is that when you call a macro, it's expanded "as is"

#define mult(a, b) a * b
mult(1+2, 3+4); // will be expanded like: 1 + 2 * 3 + 4 = 11 instead of 21.
mult_fix(a, b) ((a) * (b))

Yes, and there is one more advantage of this feature. You can leave some macro undefined and set its value as a name of another macro in the compilation command.

#define STR "string"
void main() { printf("value=%s\n", VALUE); }

In the command line you can say that the macro "VALUE" takes value from another macro "STR":

$ gcc -o test_macro -DVALUE=STR main.c
$ ./test_macro

Output:

value=string

This approach works as well for MSC compiler on Windows. I find it very flexible.