How, exactly, does the double-stringize trick work?

Posted by Peter Hosey on Stack Overflow See other posts from Stack Overflow or by Peter Hosey
Published on 2010-05-01T23:17:06Z Indexed on 2010/05/01 23:27 UTC
Read the original article Hit count: 266

Filed under:
|
|
|

At least some C preprocessors let you stringize the value of a macro, rather than its name, by passing it through one function-like macro to another that stringizes it:

#define STR1(x) #x
#define STR2(x) STR1(x)
#define THE_ANSWER 42
#define THE_ANSWER_STR STR2(THE_ANSWER) /* "42" */

Example use cases here.

This does work, at least in GCC and Clang (both with -std=c99), but I'm not sure how it works in C-standard terms.

Is this behavior guaranteed by C99?
If so, how does C99 guarantee it?
If not, at what point does the behavior go from C-defined to GCC-defined?

© Stack Overflow or respective owner

Related posts about c

    Related posts about objective-c