Consider this:
#define STRINGIFY(A) #A
If I then later write:
STRINGIFY(hello)
Is the compiler actually seeing this:
#hello
I think it is that additional hash in front of #A that is confusing me.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, the compiler will put the argument between quotes, resulting in this:
However, beware that macro substitution doesn’t take place near
#and##, so if you really need to stringify the argument (even if it’s another macro) it’s better to write two macros, the first one to expand the argument and the other one to add quotes:Or more generally, if you’re compiler supports variadic macros (introduced in C99):
If you really need a function-like macro that paste a
#at the beginning of the argument, I think you need it to generate a string (otherwise you’d make an invalid token for the compiler), so you could simple generate two string literals that the compiler will “paste”:The
mainbody will look like this in the preprocessor output:I hope this is not an obscure corner of the preprocessor that results in undefined behavior, but it shouldn’t be a problem since we’re not generating another preprocessor instruction.