I am trying to pass the name of a function-style macro as an argument to another function-style macro like this:
#include <stdio.h>
#define FOO(x, y) (x+y)
#define CALL_MACRO(macro, ...) macro(__va_args__)
int main(void) {
printf("%d\n", CALL_MACRO(FOO, 2, 3));
return 0;
}
but I get these errors:
tests.c:8:40: error: macro "FOO" requires 2 arguments, but only 1 given
8 | printf("%d\n", CALL_MACRO(FOO, 2, 3));
| ^
tests.c:3: note: macro "FOO" defined here
3 | #define FOO(x, y) (x+y)
|
tests.c:8:31: error: ‘FOO’ undeclared (first use in this function)
8 | printf("%d\n", CALL_MACRO(FOO, 2, 3));
| ^~~
tests.c:5:32: note: in definition of macro ‘CALL_MACRO’
5 | #define CALL_MACRO(macro, ...) macro(__va_args__)
| ^~~~~
tests.c:8:31: note: each undeclared identifier is reported only once for each function it appears in
8 | printf("%d\n", CALL_MACRO(FOO, 2, 3));
| ^~~
tests.c:5:32: note: in definition of macro ‘CALL_MACRO’
5 | #define CALL_MACRO(macro, ...) macro(__va_args__)
|
__va_args__instead of__VA_ARGS__. The code works if you fix that.