C Preprocessor, Stringify the result of a macro

Like this: #include <stdio.h> #define QUOTE(str) #str #define EXPAND_AND_QUOTE(str) QUOTE(str) #define TEST thisisatest #define TESTE EXPAND_AND_QUOTE(TEST) int main() { printf(TESTE); } The reason is that when macro arguments are substituted into the macro body, they are expanded unless they appear with the # or ## preprocessor operators in that macro. So, str (with value TEST … Read more

# and ## in macros

Because that is how the preprocessor works. A single ‘#’ will create a string from the given argument, regardless of what that argument contains, while the double ‘##’ will create a new token by concatenating the arguments. Try looking at the preprocessed output (for instance with gcc -E) if you want to understand better how … Read more

What are the applications of the ## preprocessor operator and gotchas to consider?

One thing to be aware of when you’re using the token-paste (‘##‘) or stringizing (‘#‘) preprocessing operators is that you have to use an extra level of indirection for them to work properly in all cases. If you don’t do this and the items passed to the token-pasting operator are macros themselves, you’ll get results … Read more

How, exactly, does the double-stringize trick work?

Yes, it’s guaranteed. It works because arguments to macros are themselves macro-expanded, except where the macro argument name appears in the macro body with the stringifier # or the token-paster ##. 6.10.3.1/1: … After the arguments for the invocation of a function-like macro have been identified, argument substitution takes place. A parameter in the replacement … Read more