Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating function names with strings using macros in C

I've two functions:

void foo0(int bar);
void foo1(int bar);

I wan't to be able create a macro QUX that will expand to these function names based on another macro BAZ. I tried the following:

#define BAZ 0
#define QUX(x) foo##BAZ(x)

But it did not work since the generated function was fooBAZ(). How can I get it to generate foo0()?

like image 900
johnmachan Avatar asked Jul 17 '13 12:07

johnmachan


1 Answers

#define CAT_I(a,b) a##b
#define CAT(a,b) CAT_I(a, b)
#define QUX(x) CAT(foo, BAZ) ## (x)
like image 98
BLUEPIXY Avatar answered Nov 11 '22 17:11

BLUEPIXY