by jstimpfle · 4y ago · 0 comments
#include <stdio.h>
#define FOO_(c) printf("%d %d %d\n", c, c, c);
#define FOO() FOO_(__COUNTER__)
#define FOO2() FOO_(__COUNTER__)
int main(void)
{
printf("%d %d %d\n", __COUNTER__, __COUNTER__, __COUNTER__);
FOO();
FOO();
FOO2();
FOO2();
return 0;
}
Output: 0 1 2
3 3 3
4 4 4
5 5 5
6 6 6IMO the best solution if you want to avoid an extra indirection to inject some state, would be preprocessor variables that you can assign to in a macro expansion. Procedural preprocessor code basically. But the preprocessor doesn't work like that.