Man, this one wasted me a lot of time. Let’s say you have a #define constant:
#define FILEVERSION 3.1.11.1
And you want to create another constant that contains the value of this constant in quotes:
#define STRFILEVERSION "3.1.11.1"
All good so far. Now let’s say you want to DRY that up a bit:
#define STRFILEVERSION "FILEVERSION"
Well, now that won’t work Jeremy. Macros inside quotes are not expanded. None of these will either:
#define STRFILEVERSION ""FILEVERSION""
#define STRFILEVERSION \"FILEVERSION\"
#define STRFILEVERSION "\""##FILEVERSION##"\""
Nope, those escape sequences just aren’t implemented in the preprocessor. And the backslash \ is meant to indicate line continuation. Aha! But what about:
#define PUTGODDAMNQUOTESAROUND(x) #x
Yes, that works. Providing the parameter you’re supplying is not… er… another macro. In other words:
PUTGODDAMNQUOTESAROUND(FILEVERSION)
returns
"FILEVERSION"
not
"3.1.1.11"
ARGH! Finally, Wikipedia to the rescue with indirect quoting of macro arguments:
#define _PUTGODDAMNQUOTESAROUND(x) #x
#define PUTGODDAMNQUOTESAROUND(x) _PUTGODDAMNQUOTESAROUND(x)
Now if you use:
PUTGODDAMNQUOTESAROUND(FILEVERSION)
at last you get
"3.1.1.11"
Choice.