The following C program tries to stringify and then print the expansions of two object-like macros:
#include <stdio.h>
#define FOOBAR foobar // I'll be gone soon :(
#define UNHOLY \" // I'm in your strings
#define STR_(X) #X
#define STR(X) STR_(X)
int main() {
printf("%s\n", STR(FOOBAR));
printf("%s\n", STR(UNHOLY));
return 0;
}
When compiled with GCC and Clang, the stringification of FOOBAR
produces the expected results. However, UNHOLY
manages to somehow retain the comment, which ends up in the string. As far as I know, comments are stripped long before the preprocessor runs, so I can only conclude that this is a lexer issue.
VC won't even compile this. It bails out with the error: escaped '"': is illegal in macro definition
. Can I simply remove the comment and assume it will work as intended (i.e. second print should produce "
) in compliant C compilers? Is VC broken again, or am I invoking some kind of unholy undefined behavior?