枚举类型的定义

  用符号而不是具体的数字来表示程序中的数字,这么表示的好处是可读性,当别人看你的程序,看到的是单词,很容易理解这些数字背后的含义,那么用什么符号来表示名字哪?需要const int常量的整数,不可修改的整型的变量。大家看下面的程序,0,1,2使用red,yellow,green代替,那么程序中就使用red,yellow,green而不是0,1,2:

 1 #include <stdio.h>
 2 
 3     const int red = 0;
 4     const int yellow = 1;
 5     const int green = 2;
 6 
 7 int main(int argc,char const *argv[]){
 8 
 9     int color = -1;
10     char *colorName = NULL;
11     
12     printf("input your favorite color:");
13     scanf("%d",&color);
14     switch(color){
15         case red: colorName = "red"; break;
16         case yellow: colorName = "yellow";break;
17         case green: colorName = "green";break;
18         default: colorName = "unknown";break;
19     }
20     printf("your favorite color is:%s\n",colorName);
21     
22     return 0;
23 }
24  
View Code

相关文章: