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

ID: Q57949


The information in this article applies to:
  • Microsoft C for MS-DOS, versions 6.0, 6.0a, 6.0ax
  • Microsoft C for OS/2, versions 6.0, 6.0a
  • Microsoft C/C++ for MS-DOS, version 7.0
  • Microsoft Visual C++ for Windows, 16-bit edition, versions 1.0, 1.5
  • Microsoft Visual C++, 32-bit Editions, versions 1.0, 2.0, 4.0, 5.0, 6.0


SUMMARY

In Microsoft C for MS-DOS and Windows NT, 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);
} 
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 query words: pound number

Keywords : kbLangC kbVC100 kbVC150 kbVC200 kbVC400 kbVC500 kbVC600
Version : MS-DOS:6.0,6.00a,6.00ax,7.0; OS/2:6.0,6.00a; WINDOWS:1.0,1.5; WINDOWS NT:1.0,2.0,4.0,5.0
Platform : MS-DOS NT OS/2 WINDOWS
Issue type :


Last Reviewed: July 1, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.