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

Table Of Contents
Preprocessor Directives and Macros
HP C/C++ Programmer’s Guide for NonStop Systems429301-010
12-17
Operator ##
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);