We help IT Professionals succeed at work.

Tricky question about macros...

vbremaud asked
Sorry for not being explicit, but you'll understand this question is difficult to sum up:

I want to write a macro that can replace abcd with "abcd" , that is transforming this line:
  cout << STRING(abcd);
  cout << "abcd";

Please note that this absolutly needs to be a macro. So, no functions, no inline functions and so on.

thanks by advance :-)

PS: I have already tried:
#define STRING(X) "X"      // I knew it wouldn't work, but well... I tried it anyway
#define STRING(X) \"X\"    // It could have worked, but the compiler didn't agree for some reason...
Watch Question

Top Expert 2012

Simply use

#define STRING(X) #X  
Top Expert 2012
To elaborate:

Stringizing Operator (#)
The number-sign or ?stringizing? operator (#) converts macro parameters (after expansion) to string constants. It is used only with macros that take arguments. If it 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 stringizing operator and 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 ignored. Any white space between the tokens in the actual argument is reduced to a single white space in the resulting string literal. Thus, if a comment occurs between two tokens in the actual argument, it is reduced to a single white space. The resulting string literal is automatically concatenated with any adjacent string literals from which it is separated only by white space.

Further, if a character contained in the argument usually requires an escape sequence when used in a string literal (for example, the quotation mark (") or backslash (\) character), the necessary escape backslash is automatically inserted before the character. The following example shows a macro definition that includes the stringizing operator and a main function that invokes the macro:

#define stringer( x ) printf( #x "\n" )

void main()
    stringer( In quotes in the printf function call\n );
    stringer( "In quotes when printed to the screen"\n );  
    stringer( "This: \"  prints an escaped double quote" );

Such invocations would be expanded during preprocessing, producing the following code:

void main()
   printf( "In quotes in the printf function call\n" "\n" );
   printf( "\"In quotes when printed to the screen\"\n" "\n" );
   printf( "\"This: \\\" prints an escaped double quote\"" "\n" );

When the program is run, screen output for each line is as follows:

In quotes in the printf function call

"In quotes when printed to the screen"

"This: \" prints an escaped double quotation mark"


Wow ! What a quick response !
Moreover, it works !!!

Thanks! I thought '#' was only to introduce macros, but it looks like a magic operator :-)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.