What I'm trying to do is to #define
a macro:
#define a(2)
and later use it inside a string literal: string = "a";
.
I want that string to be interpreted not as string but to get the value of a
, ie 2. I didn't succeed, can anybody help?
#define STRINGIFY2(X) #X
#define STRINGIFY(X) STRINGIFY2(X)
#define A 2
Then STRINGIFY(A)
will give you "2"
. You can concatenate it with other string literals by putting them side by side.
"I have the number " STRINGIFY(A) "."
gives you "I have the number 2."
.
No, you cannot do macro expansion INSIDE string literals (ie having the preprocessor to look inside literals for macros to expand).
You can have a macro expansion to produce a string literal using the stringify operator ( #
). But that's a different thing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.