c語言枚舉類型詳解

在程序中,可能需要爲某些整數定義一個別名,我們可以利用預處理指令#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<stdio.h>

 

 

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 <stdio.h>

 

 

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 <stdio.h>

 

 

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 <stdio.h>

 

 

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 <stdio.h>

 

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<stdio.h>

 

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 <stdio.h>

 

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

}

enum是基本數據類型,相當於UINT,

5. 綜合舉例

 

#include<stdio.h>

 

enum Season

{

    spring, summer=100, fall=96, winter

};

 

typedef enum

{

    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

}

Weekday;

 

void main()

{

   

    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

 

   

    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

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章