03-29-2010 09:46 AM
Hi,
Is there a way to make preprocessor accept enums to make example bellow work as it is? (I haven't digged into the ansi C spec for this)
#include <ansi_c.h>
/*
#define TEST1 (0)
#define TEST2 (1)
#define TEST3 (2)
#define TEST4 (3)
#define ALL_TESTS (4)
*/
typedef enum {TEST1, TEST2, TEST3, TEST4, ALL_TESTS} TESTS;
#define DO_TEST (TEST3)
int main(int argc, char *argv[])
{
char *names[] = {"one", "two", "three", "four"};
TESTS i;
//int i;
int index;
#if !defined(DO_TEST)
printf("Please define DO_TEST e.g. #define DO_TEST (TEST3)\n");
goto end;
#elif (DO_TEST == ALL_TESTS)
for(i=0; i<ALL_TESTS; i++)
{
#else
i=DO_TEST;
#endif
printf("name[%d] = %s\n", i, names[i]);
#if defined(DO_TEST) && (DO_TEST == ALL_TESTS)
}
#endif
end:
getchar();
return 0;
}
Solved! Go to Solution.
03-29-2010 09:57 AM
I think you're on a loser, here. enums are not part of the preprocessor subsystem of a C compiler, so of course #if will not work with them.
JR