enum 1 #include stdio.h 2 3 enum Color 4 { 5 GREEN = 1 , 6 RED, 7 BLUE, 8 GREEN_RED= 10 , 9 GREEN_BLUE 10 }ColorVal; 11 12 int main( void ) 13 { 14 printf( " sizeof(ColorVal)=%d\n " , sizeof (ColorVal)); 15 printf( " GREEN=%d\n " ,GREEN); 1
enum
1 #include <stdio.h> 2 3 enum Color 4 { 5 GREEN = 1, 6 RED, 7 BLUE, 8 GREEN_RED=10, 9 GREEN_BLUE 10 }ColorVal; 11 12 int main(void) 13 { 14 printf("sizeof(ColorVal)=%d\n",sizeof(ColorVal)); 15 printf("GREEN=%d\n",GREEN); 16 printf("RED=%d\n",RED); 17 printf("BLUE=%d\n",BLUE); 18 printf("GREEN_RED=%d\n",GREEN_RED); 19 printf("GREEN_BLUE=%d\n",GREEN_BLUE); 20 return 0; 21 }
运行结果:
sizeof(ColorVal)=4 GREEN=1 RED=2 BLUE=3 GREEN_RED=10 GREEN_BLUE=11
enum变量类型可以给其中的常量符号赋值,如果不赋值则会从被赋初始值的那个常量开始依次+1;如果都没有赋值,它们的值从0开始依次+1。