1
enum boolean {NO, YES};
printf("%d\n", NO);
printf("%d\n", YES);
0
1
2
enum boolean {NO, YES};
enum boolean2 {YES};
one.c: In function 'main':
one.c:7:16: error: redeclaration of enumerator 'YES'
enum boolean2 {YES};
^~~
one.c:6:20: note: previous definition of 'YES' was here
enum boolean {NO, YES};
3
enum boolean {NO=3, YES, UNKNOWN};
printf("%d\n", NO);
printf("%d\n", YES);
printf("%d\n", UNKNOWN);
3
4
5
结论:
- 不赋值,默认从 0 开始,然后递增。
- 不允许重名。
- 没有赋值的枚举变量,值为前一个枚举值加一。