-1

As you can see concatenating a token works,
also a token being another macro works,
but when a token is a macro it doesn't seem to work?

longNameForaFunction_one(){return 1;}
longNameForaFunction_two(){return 2;}
longNameForaFunction_third(){return 3;}
two(){return 2;}
#define bar two
#define foo(x)(longNameForaFunction_##x())
#define three third
main(){
printf("%d\n",foo(one)); // 1

printf("%d\n",foo(two)); // 2

printf("%d\n",bar()); // 2

// printf("%d\n",foo(three)); // this doesn't work  
}

The last row gives this error if uncommented;

undefined reference to `longNameForaFunction_three'

#define three third

Seems to have no effect

Try it online

rici
  • 234,347
  • 28
  • 237
  • 341
PrincePolka
  • 196
  • 1
  • 9
  • 1
    Use more white space. Use `#define EVALUATOR(x) x` and `#define CONCATENATE(x, y) x ## y` and then `#define foo(x) CONCATENATE(longNameForaFunction_, EVALUATOR(x))`. And this is a duplicate. – Jonathan Leffler Dec 30 '17 at 05:58

1 Answers1

1

Well that's why you need to provide another level before it works - the macro parameter will be expanded before it is passed to foo.

#define foo(x)(longNameForaFunction_##x())
#define foo1(x) foo(x)
#define three third

..

printf("%d\n",foo1(three));
user2736738
  • 30,591
  • 5
  • 42
  • 56