C言語enum列挙タイプ解析

9484 ワード

      ,            。             ,          。  C                         。                 ,                 。 
           ,            ,               。
  : 
enum weekday{sun,mon,tue,wed,thu,fri,sat}; 
           enum weekday,            。  : 
enum weekday day; 
  ,             。  : 
enum weekday{sun,mon,tue,wed,thu,fri,sat} day; 
  ,sum,mon,…,sat             ,           。 
          。 
①         ,    ,             。     ,             。 
②         ,      ,C                   ,1,2,…。 
       ,sun     0,mon     1,…sat     6,        
day=mon; 
  day       1。  ,           。  : 
printf ("%d",day); 
      1。 
                ,           。  : 
enum weekday{sun=7,mon=1,tue,wed,thu,fri,sat}day; 
  ,sun   7,mon   1,        1,   sat    6  。 
③           。  : 
if (day==mon) {…} 
if (day>mon) {…} 
         :            ,           ,             0。  ,mon>sun,sat>fri。 
C      ?216? 
④                 ,              。  : 
day=(enum weekday)2; 
        ,      2         day,   workday=tue; 
【  11.6】         ,                  。 
# include 
void main( ) 
{ 
enum weekday {sun,mon,tue,wed,thu,fri,sat} day; 
int k; 
printf("input a number(0--6)"); 
scanf("%d",&k); 
day=(enum weekday)k; 
switch(day) 
{ 
case sun: printf("sunday/n");break; 
case mon: printf("monday/n");break; 
case tue: printf("tuesday/n");break; 
case wed: printf("wednesday/n");break; 
case thu: printf("thursday/n");break; 
case fri: printf("friday/n");break; 
case sat: printf("satday/n");break; 
default: printf("input error/n");break; 
} 
} 
       : 
input a number(0--6)1 
monday 
     ,               ,               ,        : 
printf(" %s",mon); 
       mon     ,     。 
        ,             ,        。
 
------------------------------------------------------------------
 :            VC++ 6.0
    ,               ,           #define       ,       :

#define MON  1
#define TUE   2
#define WED  3
#define THU   4
#define FRI    5
#define SAT   6
#define SUN   7
 
  ,            ,           。            。
 
1.            -    
                -    

enum DAY
{
      MON=1, TUE, WED, THU, FRI, SAT, SUN
};
 
(1)         ,      (    )          ,       ,  。
(2) DAY      ,           ,      ,        。
(3)                0,                1。
(4)             ,             。
(5)          #define   。
(6)        ;  。
 
2.              
           ,       。              , :  int,       float,       double,    char,    short  。                  :

char     a; //  a        char
char     letter;
int        x,
           y,
           z; //  x,y z       int
int       number;
double  m, n;
double  result; //  result          double
 
            ,                      。
   :               

enum DAY
{
      MON=1, TUE, WED, THU, FRI, SAT, SUN
};
 

enum DAY yesterday;
enum DAY today;
enum DAY tomorrow; //   tomorrow       enum DAY
enum DAY good_day, bad_day; //  good_day bad_day        enum DAY
 
   :             :

enum //          ,     DAY  ,     。
{
    saturday,
    sunday = 0,
    monday,
    tuesday,
    wednesday,
    thursday,
    friday
} workday; //  workday       enum DAY
 

enum week { Mon=1, Tue, Wed, Thu, Fri Sat, Sun} days; //  days       enum week
 

enum BOOLEAN { false, true } end_flag, match_flag; //                 
 
   : typedef             ,            :

typedef enum workday
{
    saturday,
    sunday = 0,
    monday,
    tuesday,
    wednesday,
    thursday,
    friday
} workday; //   workday    enum workday   
 

workday today, tomorrow; //  today tomorrow       workday,  enum workday
 
enum workday  workday    :

typedef enum
{
    saturday,
    sunday = 0,
    monday,
    tuesday,
    wednesday,
    thursday,
    friday
} workday; //   workday    enum workday   
workday today, tomorrow; //  today tomorrow       workday,   enum workday
 
        :

typedef enum workday
{
    saturday,
    sunday = 0,
    monday,
    tuesday,
    wednesday,
    thursday,
    friday
};
workday today, tomorrow; //  today tomorrow       workday,   enum workday
 
  :                 ,                    。        :
     :         

typedef enum
{
    wednesday,
    thursday,
    friday
} workday;
typedef enum WEEK
{
    saturday,
    sunday = 0,
    monday,
} workday;
 
     :         

typedef enum
{
    wednesday,
    thursday,
    friday
} workday_1;
typedef enum WEEK
{
    wednesday,
    sunday = 0,
    monday,
} workday_2;
 
 
3.          
3.1          。
                         :
   :     ,      

#include
/*        */
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };
void main()
{
    /*             ,        */
    int x, y, z;
    
    x = 10;
    y = 20;
    z = 30;
    
    /*           ,          */
    enum DAY yesterday, today, tomorrow;
    
    yesterday = MON;
    today     = TUE;
    tomorrow  = WED;
    printf("%d %d %d /n", yesterday, today, tomorrow);
}
 
   :          

#include 
/*        */
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };
void main()
{
    /*                      */
    int x=10, y=20, z=30;
    /*                       */
    enum DAY yesterday = MON, 
                        today = TUE,
                   tomorrow = WED;
    printf("%d %d %d /n", yesterday, today, tomorrow);
}
 
   :           ,       。

#include 
/*       ,            ,         */
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN } yesterday, today, tomorrow;
/*                ,         */
int x, y, z;
void main()
{
    /*              */
    x = 10;  y = 20;  z = 30;
    
    /*           */
    yesterday = MON;
    today     = TUE;
    tomorrow  = WED;
    printf("%d %d %d /n", x, y, z); //  :10 20 30
    printf("%d %d %d /n", yesterday, today, tomorrow); //  :1 2 3
}
 
   :    ,    ,       。

#include 
/*       ,            ,    。         */
enum DAY
{
    MON=1, 
    TUE,
    WED,
    THU,
    FRI,
    SAT,
    SUN 
}
yesterday = MON, today = TUE, tomorrow = WED;
/*                ,    。         */
int x = 10, y = 20, z = 30;
void main()
{
    printf("%d %d %d /n", x, y, z); //  :10 20 30
    printf("%d %d %d /n", yesterday, today, tomorrow); //  :1 2 3
}
 
3.2             ,        。

#include 
enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };
void main()
{
    enum DAY yesterday, today, tomorrow;
    yesterday = TUE;
    today = (enum DAY) (yesterday + 1); //    
    tomorrow = (enum DAY) 30; //    
    //tomorrow = 3; //  
    printf("%d %d %d /n", yesterday, today, tomorrow); //  :2 3 30
}
 
3.3        

#include
enum
{ 
    BELL          = '/a',
    BACKSPACE = '/b',
    HTAB         = '/t',
    RETURN      = '/r',
    NEWLINE    = '/n', 
    VTAB         = '/v',
    SPACE       = ' '
};
enum BOOLEAN { FALSE = 0, TRUE } match_flag;
void main()
{
    int index = 0;
    int count_of_letter = 0;
    int count_of_space = 0;
    char str[] = "I'm Ely efod";
    match_flag = FALSE;
    for(; str[index] != '/0'; index++)
        if( SPACE != str[index] )
            count_of_letter++;
        else
        {
            match_flag = (enum BOOLEAN) 1;
            count_of_space++;
        }
    
    printf("%s %d times %c", match_flag ? "match" : "not match", count_of_space, NEWLINE);
    printf("count of letters: %d %c%c", count_of_letter, NEWLINE, RETURN);
}
 
  :
match 2 times
count of letters: 10
Press any key to continue
 
4.      sizeof   

#include 
enum escapes
{ 
    BELL      = '/a',
    BACKSPACE = '/b',
    HTAB      = '/t',
    RETURN    = '/r',
    NEWLINE   = '/n', 
    VTAB      = '/v',
    SPACE     = ' '
};
enum BOOLEAN { FALSE = 0, TRUE } match_flag;
void main()
{
    printf("%d bytes /n", sizeof(enum escapes)); //4 bytes
    printf("%d bytes /n", sizeof(escapes)); //4 bytes
    printf("%d bytes /n", sizeof(enum BOOLEAN)); //4 bytes
    printf("%d bytes /n", sizeof(BOOLEAN)); //4 bytes
    printf("%d bytes /n", sizeof(match_flag)); //4 bytes
    printf("%d bytes /n", sizeof(SPACE)); //4 bytes
    printf("%d bytes /n", sizeof(NEWLINE)); //4 bytes
    printf("%d bytes /n", sizeof(FALSE)); //4 bytes
    printf("%d bytes /n", sizeof(0)); //4 bytes
}
 
5.     

#include
enum Season
{
    spring, summer=100, fall=96, winter
};
typedef enum
{
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
Weekday;
void main()
{
    /* Season */
    printf("%d /n", spring); // 0
    printf("%d, %c /n", summer, summer); // 100, d
    printf("%d /n", fall+winter); // 193
    Season mySeason=winter;
    if(winter==mySeason)
        printf("mySeason is winter /n"); // mySeason is winter
    
    int x=100;
    if(x==summer)
        printf("x is equal to summer/n"); // x is equal to summer
    printf("%d bytes/n", sizeof(spring)); // 4 bytes
    /* Weekday */
    printf("sizeof Weekday is: %d /n", sizeof(Weekday)); //sizeof Weekday is: 4
    Weekday today = Saturday;
    Weekday tomorrow;
    if(today == Monday)
        tomorrow = Tuesday;
    else
        tomorrow = (Weekday) (today + 1); //remember to convert from int to Weekday
}