1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #define FOREACH_FRUIT(FRUIT) \ FRUIT(apple) \ FRUIT(orange) \ FRUIT(grape) \ FRUIT(banana) \
#define GENERATE_ENUM(ENUM) ENUM, #define GENERATE_STRING(STRING) #STRING,
enum FRUIT_ENUM { FOREACH_FRUIT(GENERATE_ENUM) };
static const char *FRUIT_STRING[] = { FOREACH_FRUIT(GENERATE_STRING) };
// After the preprocessor gets done, you'll have: // enum FRUIT_ENUM { // apple, orange, grape, banana, // };
// static const char *FRUIT_STRING[] = { // "apple", "orange", "grape", "banana", // }; printf("enum apple as a string: %s\n",FRUIT_STRING[apple]);
|