Tag Archives: #define

stringify the result of expansion of a macro argument

To strignify a c/c++ macro’s argument you just have to add a leading ‘#’ in macro’s parameter.

#define STRINGIFY(s) #s

const char * thestring = STRINGIFY(50);

The preprocessor will expand this to:

const char * thestring = "50";

But if you try to pass a macro as argument to the STRINGIFY(s) macro, the macro argument will not expand to it’s value.

#define STRINGIFY(s) #s
#define VALUE 50

const char * thestring = STRINGIFY(VALUE);

will expand to:

const char * thestring = "VALUE";

That’s because stringification and concatenation use the argument as written, in un-prescanned form.

If you want to stringify the result of a macro argument, you have to use two levels of macros.

#define STRINGIFY(s) XSTR(s)
#define XSTR(s) #s
#define VALUE 50

const char * thestring = STRINGIFY(VALUE);

so, that will expand as:

const char * thestring = "50";

resource:  Stringification – The C Preprocessor