INF: Use of the Stringizing Operator (#) in Macros

ID Number: Q57949

3.x 4.x 5.x 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

In Microsoft C versions 5.0, 5.1, 6.0, 6.0a, 6.0ax, and C/C++ version

7.0, when you are writing a preprocessor macro that takes an argument

that must appear in quotation marks, you can use the # sign to expand

the argument. One implementation of this preprocessor directive is the

use of printf() in the macro. The following code demonstrates an

example:

Sample Code

-----------

#define PR(fmt,value) printf("value = %" #fmt "\n", (value))

#include <stdio.h>

void main(void)

{

float afl;

afl = 3.14f;

PR(5.2f, afl);

}

More Information:

The sample code outputs the following string:

value = 3.14

The # sign in front of the fmt variable allows the macro to be

expanded using quotation marks. Note that the preprocessor

concatenates consecutive pairs of double quotation marks so that the

following string

"value = %""5.2f""\n"

is translated into the following:

"value = %5.2f\n"

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00 pound

number