Amine Amine - 1 year ago 95
C++ Question

Expand macro inside string literal

What I'm trying to do is to

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
, i.e. 2. I didn't succeed, can anybody help?

Answer Source
#define STRINGIFY2(X) #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.".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download