E.7 Operators # and ##
The #
and ##
preprocessor operators are available in C++ and ANSI/ISO C. The #
operator causes a replacement-text token to be converted to a string surrounded by quotes. Consider the following macro definition:
#define HELLO( x ) cout << "Hello, " #x << endl;
When HELLO(John)
appears in a program file, it is expanded to
cout << "Hello, " "John" << endl;
The string "John"
replaces #x
in the replacement text. Strings separated by white space are concatenated during preprocessing, so the above statement is equivalent to
cout << "Hello, John" << endl;
Note that the #
operator must be used in a macro with arguments, because the operand of #
refers to an argument of the macro.
The ##
operator concatenates two tokens. Consider ...
Get C++ How to Program, 10/e now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.