说明:
    枚举型是预处理指令#define的替代。
    枚举型是一个集合,集合中的元素(枚举成员)是一些命名的整型常量,元素之间用逗号,隔开。
    类型定义以分号;结束。
    第一个枚举成员的默认值为整型的0,后续枚举成员的值在前一个成员上加1。
 

联发科的一道笔试题:

 
  
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. using namespace std;  
  4.  
  5. enum value  
  6. {  
  7.     value1,  
  8.     value2,  
  9.     value3=0,  
  10.     value4=50,  
  11.     value5,  
  12. };  
  13.  
  14. void main()  
  15. {  
  16.     int i=1;  
  17.  
  18.     do{  
  19.         cout<<value5+i<<",";  
  20.         i++;  
  21.         if(i<4)  
  22.         continue;  
  23.         break;  
  24.     }while(value2);  
  25.  
  26. }  

枚举与sizeof

 
  
  1. #include <stdio.h>  
  2.  
  3. enum WEEK  
  4. {  
  5.     sat=2,  
  6.     sun,  
  7.     mon=11,  
  8.     tue,      
  9. };  
  10.  
  11. int main(void)  
  12. {  
  13.     printf("%d %d %d\n",sun,tue,sizeof(tue));   //3 12 4  
  14.     printf("%d %d %d\n",sizeof(0),sizeof(WEEK),sizeof(sat)); //4 4 4  
  15.     return 0;  

 

 
  
  1. #include<stdio.h>  
  2.  
  3. enum 
  4. {   
  5.     BELL          = '\a',  
  6.     BACKSPACE = '\b',  
  7.     HTAB         = '\t',  
  8.     RETURN      = '\r',  
  9.     NEWLINE    = '\n',   
  10.     VTAB         = '\v',  
  11.     SPACE       = ' ' 
  12. };  
  13.  
  14. enum BOOLEAN { FALSE = 0, TRUE } match_flag;  
  15.  
  16. void main()  
  17. {  
  18.    
  19.     int index = 0;  
  20.     int count_of_letter = 0;  
  21.     int count_of_space = 0;  
  22.  
  23.     char str[] = "I'm Ely efod";  
  24.  
  25.     match_flag = FALSE;  
  26.  
  27.     for(; str[index] != '\0'; index++)  
  28.         if( SPACE != str[index] )  
  29.             count_of_letter++;  
  30.         else 
  31.         {  
  32.             match_flag = (enum BOOLEAN) 1;  
  33.             count_of_space++;  
  34.         }  
  35.       
  36.     printf("%s %d times %c", match_flag ? "match" : "not match", count_of_space, NEWLINE);  
  37.     printf("count of letters: %d %c%c", count_of_letter, NEWLINE, RETURN);