This is a follow-up question to this one (and also closer to the actual problem at hand).
If I have the following case:
#include <stdio.h>
#define FOO_ONE 12
#define FOO_TWO 34
#define BAR_ONE 56
#define BAR_TWO 78
#define FOO 99
#define STRINGIFY(mac) #mac
#define CONCAT(mac1, mac2) STRINGIFY(mac1) STRINGIFY(mac2)
#define MAKE_MAC(mac) CONCAT(mac##_ONE, mac##_TWO)
#define PRINT(mac) printf(#mac ": " MAKE_MAC(mac) "\n")
void main(int argc, char *argv[])
{
PRINT(FOO);
PRINT(BAR);
}
As can be seen, the stringified, concatenated macros are then substituted inside a printf()
statement which itself is inside a macro.
Because FOO
is defined (as 99
), it happens that it is expanded before the concatenation with _ONE
and _TWO
, effectively creating the tokens 99_ONE
and 99_TWO
.
This program outputs:
FOO: 99_ONE99_TWO
BAR: 5678
How can I defer the expansion of the FOO macro (effectively, eliminating it altogether, to get the required output of:
FOO: 1234
BAR: 5678
NOTE: assume the PRINT()
macro signature cannot be changed (i.e., can't add a parameter, etc.). However its implementation can be changed. Also, FOO
, FOO_*
and BAR_*
definitions cannot be modified as well.