2

Say I have this:

#define CAKE     , something

and the result I want is ", something". Can it be done?

The following doesn't work in gcc:

#define MAKE_STRING(x)  #x
#define STRING(x)       MAKE_STRING(x)

STRING(CAKE)

The compiler thinks I'm passing two arguments into MAKE_STRING() and balks.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
cleong
  • 7,242
  • 4
  • 31
  • 40

2 Answers2

6

If your preprocessor supports variadic macros, __VA_ARGS__ will do the trick:

#define CAKE     , something

#define MAKE_STRING(...)  #__VA_ARGS__
#define STRING(x)       MAKE_STRING(x)

#include <stdio.h>
int main()
{
    printf("%s\n", STRING(CAKE) );
}
jfly
  • 7,715
  • 3
  • 35
  • 65
0
#define CAKE     (, something)

please have a try.

BlackMamba
  • 10,054
  • 7
  • 44
  • 67