C/C++ Programmer's Guide (G06.27+, H06.08+, J06.03+)

allows you to concatenate two tokens in a macro definition. Both of these operators are used in
the context of the #define directive.
Operator #
The unary operator # is used only with function-like macros, which take arguments. If the # operator
precedes a formal parameter in the macro definition, the actual argument passed by the macro
invocation is enclosed in quotation marks and treated as a string literal. The string literal then
replaces each occurrence of a combination of the # operator and the formal parameter within the
macro definition.
White space preceding the first token of the actual argument and following the last token of the
actual argument is deleted. If a character contained in the argument normally requires an escape
sequence when used in a string literal, for example the quotation mark (") or backslash (\)
characters, the necessary escape backslash is automatically inserted before the character.
Example
#define str(x) # x
causes the invocation:
str(testing)
to be expanded into:
"testing"
Operator ##
The binary operator ## is used in both object-like and function-like macros. It allows you to
concatenate two tokens, and therefore it cannot occur at the beginning or at the end of the macro
definition.
If a formal parameter in a macro definition is preceded or followed by the ## operator, the formal
parameter is immediately replaced by the unexpanded actual argument. Macro expansion is not
performed on the argument prior to replacement. Then, each occurrence of the ## operator in the
replacement-list is removed, and the tokens preceding it and following it are concatenated. The
resulting token must be a valid token. If it is, the resulting token is available for further macro
replacement.
Example
#define debug(s, t) printf("x" # s "= %d, x" # t "= %s",\
x ## s, x ## t )
main ()
{
debug(1, 2);
}
After the preprocessor pass, you have:
int main(void)
{
...printf("x1= %d, x2= %s", x1, x2);
Preprocessor Operators 165