C语言枚举值和标识符
示例
/* declare items of the enum */
#define FOREACH \
X(item1) \
X(item2) \
X(item3) \
/* end of list */
/* define the enum values */
#define X(id) MyEnum_ ## id,
enum MyEnum { FOREACH };
#undef X
/* convert an enum value to its identifier */
const char * enum2string(int enumValue)
{
const char* stringValue = NULL;
#define X(id) if (enumValue == MyEnum_ ## id) stringValue = #id;
FOREACH
#undef X
return stringValue;
}接下来,您可以在代码中使用枚举值,并使用以下命令轻松打印其标识符:
printf("%s\n", enum2string(MyEnum_item2));
热门推荐
10 长辈生日横幅祝福语简短
11 对同事简短生日祝福语
12 祝产妇的祝福语简短
13 给司机老师祝福语简短
14 生了女孩祝福语简短的
15 下雪路滑祝福语简短
16 春节送祝福语简短的
17 作文结尾新年祝福语简短
18 简短的宝宝出生祝福语