如何在 C 中枚举所有 enum
名称和值以像打印一样
printf("Name: %s, value: %d\n", name, value);
?
最佳答案
查看 the X macro :
#define COLORS \
X(Cred, "red") \
X(Cblue, "blue") \
X(Cgreen, "green")
#define X(a, b) a,
enum Color { COLORS };
#undef X
#define X(a, b) b,
static char *ColorStrings[] = { COLORS };
#undef X
printf("%s\n", ColorStrings[Cred]); // output: red
关于c - 枚举 C 中的所有枚举名称/值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20032373/