0

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. "my_func".

I tried using the hash preprocessor operator like so:

#define str(s) #s
#define MY_FUNC_NAME_LITERAL str(MY_FUNC_NAME)

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

Any ideas how to achieve that?

Albus Dumbledore
  • 12,368
  • 23
  • 64
  • 105

1 Answers1

2

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"

Actual answer for this problem:

#define str_(s) #s
#define str(s) str_(s)
#define MY_FUNC_NAME_LITERAL str(MY_FUNC_NAME)
#define MY_FUNC_NAME my_func
Habi Sheriff
  • 179
  • 1
  • 1
  • 7
Stargateur
  • 24,473
  • 8
  • 65
  • 91