Q: I'm trying to use the ANSI ``stringizing'' preprocessing operator `#' to insert the value of a symbolic constant into a message, but it keeps stringizing the macro's name rather than its value.
A: It turns out that the definition of # says that it's supposed to stringize a macro argument immediately, without further expanding it (if the argument happens to be the name of another macro). You can use something like the following two-step procedure to force a macro to be expanded as well as stringized:
#define Str(x) #x #define Xstr(x) Str(x) #define OP plus char *opname = Xstr(OP);This code sets opname to "plus" rather than "OP". (It works because the Xstr() macro expands its argument, and then Str() stringizes it.) [footnote]
An equivalent circumlocution is necessary with the token-pasting operator ## when the values (rather than the names) of two macros are to be concatenated.
Note that both # and ## operate only during preprocessor macro expansion. You cannot use them in normal source code, but only in macro definitions.
ISO Sec. 188.8.131.52, Sec. 184.108.40.206