Is there a way to get the exact words passed into the #define without stringifying? Example use case:
#define NUM 1
#define CREATE_FUN(X) \
void prefix_X() { \ // used exact words passed in
int x = X; \ // use value later on
}
CREATE_FUN(NUM)
And output would look like:
void prefix_NUM() {
int x = 1;
}
Use the token concatenation (##):
#define NUM 1
#define CREATE_FUN(X) \
void prefix_##X() { \
int x = X; \
}
CREATE_FUN(NUM)
#define CREATE_FUN(X) \
int prefix_##X() { \
int x = X; \
return x; \
}
CREATE_FUN(NUM)
You can use Token Pasting(##) operator:
#define CREATE_FUN(X) int prefix_##X() { int x=X; return x;}
Sample use case:
Some compilers provide an extension that allows
##to appear after a comma and before__VA_ARGS__, in which case
the
##does nothing when VA_ARGS is non-emptyremoves the comma when VA_ARGS is empty.
This makes it possible to define macros with variable number of arguments such as fprintf (stderr, format, ##__VA_ARGS__)