If you could use an enum instead of #defines, life is simpler.
Or you can indirectly count the #defines so long as you're prepared to put them in an array.
Or you use an external program to count them, and make a #define on the command line.
Code:
#include<stdio.h>
#define TEST1 0x10u
#define TEST2 0x15u
#define TEST3 0x32u
// place all the defines you want to count in here
int dummy[] = {
TEST1,
TEST2,
TEST3
};
enum {
T1,
T2,
T3,
TMAX,
};
char array[sizeof(dummy)/sizeof(*dummy)];
char arr2[TMAX];
char arr3[TEST_MAX]; // comes from -D on the command line
int main(void)
{
printf("%zd\n", sizeof(array));
printf("%zd\n", sizeof(arr2));
printf("%zd\n", sizeof(arr3));
return 0;
}
// Must be compiled with
gcc -DTEST_MAX=$(egrep -c '#define TEST' foo.c) foo.c