Not in a loop, but with the predefined __COUNTER__ you can do the following.
Build a macro that defines the declaration and function call using the counter parameter ( CALLFUNC0 ), so the counter can be used twice without increasing it.
Then create a macro chain that calls the previous macro twice ( CALLFUNC2 , CALLFUNC4 , ...) For 30 functions up to CALLFUNC16 (16 + 8 + 4 + 2 = 30).
Then define the function using the macro CALLFUNC30 . The CALLFUNC30 macro can only be used once, because __COUNTER__ incremented each time it is used.
In my example, the function names void myfuncx1(int, int) are void myfuncx30(int, int)
#define PASTE0(x,y) x ## y #define PASTE1(x,y) PASTE0(x,y) extern void callmyfuncs(int arg1, int arg2);
/* callmyfunctions.c */ #include "callmyfunctions.h" _Static_assert(__COUNTER__ == 0, "__COUNTER__ must not be used before"); #define CALLFUNC0(func, counter, ...) extern void PASTE1(func,counter)(int arg1, int arg2);\ PASTE1(func,counter)(__VA_ARGS__); #define CALLFUNC1(func, ...) CALLFUNC0(func, __COUNTER__, __VA_ARGS__) #define CALLFUNC2(func, ...) CALLFUNC1(func, __VA_ARGS__) CALLFUNC1(func, __VA_ARGS__) #define CALLFUNC4(func, ...) CALLFUNC2(func, __VA_ARGS__) CALLFUNC2(func, __VA_ARGS__) #define CALLFUNC8(func, ...) CALLFUNC4(func, __VA_ARGS__) CALLFUNC4(func, __VA_ARGS__) #define CALLFUNC16(func, ...) CALLFUNC8(func, __VA_ARGS__) CALLFUNC8(func, __VA_ARGS__) #define CALLFUNC30(func, ...) CALLFUNC16(func, __VA_ARGS__) CALLFUNC8(func, __VA_ARGS__) \ CALLFUNC4(func, __VA_ARGS__) CALLFUNC2(func, __VA_ARGS__) void callmyfuncs(int arg1, int arg2) { CALLFUNC30(myfuncx, arg1, arg2) }
#include <stdio.h> #include "callmyfunctions.h" _Static_assert(__COUNTER__ == 0, "__COUNTER__ must not be used before"); #define DEFINEFUNC1(func) void PASTE1(func,__COUNTER__)(int arg1, int arg2) {\ printf("%s %d %d\n", __func__, arg1, arg2); \ } #define DEFINEFUNC2(func) DEFINEFUNC1(func) DEFINEFUNC1(func) #define DEFINEFUNC4(func) DEFINEFUNC2(func) DEFINEFUNC2(func) #define DEFINEFUNC8(func) DEFINEFUNC4(func) DEFINEFUNC4(func) #define DEFINEFUNC16(func) DEFINEFUNC8(func) DEFINEFUNC8(func) #define DEFINEFUNC30(func) DEFINEFUNC16(func) DEFINEFUNC8(func) \ DEFINEFUNC4(func) DEFINEFUNC2(func) DEFINEFUNC30(myfuncx) int main(void) { callmyfuncs(0,1); callmyfuncs(2,3); return 0; }
$ gcc callmyfunctions.c callmyfunctions_test.c -Wall -Wextra
$ ./a.out myfuncx1 0 1 myfuncx2 0 1 myfuncx3 0 1 myfuncx4 0 1 myfuncx5 0 1 myfuncx6 0 1 myfuncx7 0 1 myfuncx8 0 1 myfuncx9 0 1 myfuncx10 0 1 myfuncx11 0 1 myfuncx12 0 1 myfuncx13 0 1 myfuncx14 0 1 myfuncx15 0 1 myfuncx16 0 1 myfuncx17 0 1 myfuncx18 0 1 myfuncx19 0 1 myfuncx20 0 1 myfuncx21 0 1 myfuncx22 0 1 myfuncx23 0 1 myfuncx24 0 1 myfuncx25 0 1 myfuncx26 0 1 myfuncx27 0 1 myfuncx28 0 1 myfuncx29 0 1 myfuncx30 0 1 myfuncx1 2 3 myfuncx2 2 3 myfuncx3 2 3 myfuncx4 2 3 myfuncx5 2 3 myfuncx6 2 3 myfuncx7 2 3 myfuncx8 2 3 myfuncx9 2 3 myfuncx10 2 3 myfuncx11 2 3 myfuncx12 2 3 myfuncx13 2 3 myfuncx14 2 3 myfuncx15 2 3 myfuncx16 2 3 myfuncx17 2 3 myfuncx18 2 3 myfuncx19 2 3 myfuncx20 2 3 myfuncx21 2 3 myfuncx22 2 3 myfuncx23 2 3 myfuncx24 2 3 myfuncx25 2 3 myfuncx26 2 3 myfuncx27 2 3 myfuncx28 2 3 myfuncx29 2 3 myfuncx30 2 3