I have been given a library that defines the following macros:
#define FOO_0(A, B) (A + B)
#define FOO_1(A, B) (A - B)
Now I'd like to create a new macro MY_FOO that takes a third argument x and use it build the name of the macro to be called (e.g. FOO_<x>)
Here is my experiment:
#define MY_FOO(X, A, B) FOO_## X ##(A, B)
However, when I try to use it in my code:
int main(void) {
int a = 2, b = 3, x = 0;
printf("FOO_%d(%d, %d) = %d", x, a, b, MY_FOO(x, a, b));
return 0;
}
I get the following error:
prog.c: In function ‘main’:
prog.c:6:25: error: pasting "FOO_x" and "(" does not give a valid preprocessing token
#define MY_FOO(X, A, B) FOO_## X ##(A, B)
^
prog.c:11:41: note: in expansion of macro ‘MY_FOO’
printf("FOO_%d(%d, %d) = %d", x, a, b, MY_FOO(x, a, b));
^~~~~~
prog.c:6:25: warning: implicit declaration of function ‘FOO_x’ [-Wimplicit-function-declaration]
#define MY_FOO(X, A, B) FOO_## X ##(A, B)
^
prog.c:11:41: note: in expansion of macro ‘MY_FOO’
printf("FOO_%d(%d, %d) = %d", x, a, b, MY_FOO(x, a, b));
^~~~~~
Is there a way to workaround this?
##is not required.