Albus Dumbledore Albus Dumbledore - 1 year ago 70
C Question

Creating a string literal from a define

Lets say I have a define like so:

#define MY_FUNC_NAME my_func

I want to get a string literal for it, e.g.

I tried using the hash preprocessor operator like so:

#define str(s) #s

But this would yield
instead of
, as though it does not replace it before creating the literal.

Any ideas how to achieve that?

Answer Source

Like the GCC documentation explains, you need two macros to do that

 #define xstr(s) str(s)
 #define str(s) #s
 #define foo 4
 str (foo)
      ==> "foo"
 xstr (foo)
      ==> xstr (4)
      ==> str (4)
      ==> "4"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download