Macro Tricks
Stringification
The # operator allows to create a string out of a macro parameter. With the ‘stringize’ trick any defined constant can be converted into a string literal.
#define stringize(s) _stringize(s)
#define _stringize(s) #s
#define IMPORTANT_CONST 4If you’d use the ‘stringize’ operator # directly in a macro you won’t get the intended string:
_stringize(IMPORTANT_CONST)would lead to:
"IMPORTANT_CONST"That’s why there is another macro calling the first one:
stringize(IMPORTANT_CONST)is replaced to:
stringize(4) which leads to:
_stringize(4) and finally:
"4" Concatenation
With the ## operator in a preprocessor macro it’s possible to combine two tokens.
#define CREATE_ID(name) ID_##name
CREATE_ID(IMPORTANT_THING)ID_IMPORTANT_THINGFurther Reading
There is a good explanation in the GCC online docs: Macros