轉載 - C - 枚舉類型詳解

出處:http://www.cnblogs.com/JCSU/articles/1299051.htmlhtml

注:如下所有代碼的執行環境爲VC++ 6.0spring

在程序中,可能須要爲某些整數定義一個別名,咱們能夠利用預處理指令#define來完成這項工做,您的代碼多是:spa

複製代碼
#define MON  1
#define TUE   2
#define WED  3
#define THU   4
#define FRI    5
#define SAT   6
#define SUN   7
複製代碼

 

在此,咱們定義一種新的數據類型,但願它能完成一樣的工做。這種新的數據類型叫枚舉型。code

 

1. 定義一種新的數據類型 - 枚舉型htm

 如下代碼定義了這種新的數據類型 - 枚舉型blog

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

 

(1) 枚舉型是一個集合,集合中的元素(枚舉成員)是一些命名的整型常量,元素之間用逗號,隔開。rem

(2) DAY是一個標識符,能夠當作這個集合的名字,是一個可選項,便是無關緊要的項。get

(3) 第一個枚舉成員的默認值爲整型的0,後續枚舉成員的值在前一個成員上加1。io

(4) 能夠人爲設定枚舉成員的值,從而自定義某個範圍內的整數。class

(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
}
複製代碼

 

5. 綜合舉例

複製代碼
#include<stdio.h>

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
}
複製代碼
相關文章
相關標籤/搜索