我如何枚举enum
C中的所有名称和值以将其打印为
printf("Name: %s, value: %d\n", name, value);
?
查看X宏:
#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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句