我們可以將枚舉理解為編譯階段的宏,使用格式:
enum typeName { valueName1, valueName2, valueName3, ...... };
typeName是枚舉類型的名字,花括號里面的元素(枚舉成員)是常量而不是變量,這個一定要搞清楚,因為枚舉成員的是常量,所以不能對它們賦值,只能將它們的值賦給其他的變量。
枚舉是 C 語言中的一種基本數據類型,它可以讓數據更簡潔,更易讀。
接下來我們舉個例子,比如:一星期有 7 天,如果不用枚舉,我們需要使用 #define 來為每個整數定義一個別名:
#defineMON 1
#defineTUE 2
#defineWED 3
#defineTHU 4
#defineFRI 5
#defineSAT 6
#defineSUN 7
這個看起來代碼量就比較多,接下來我們看看使用枚舉的方式:
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
這樣看起來是不是更簡潔了。
需要注意的兩點是:
1) 枚舉列表中的 Mon、Tues、Wed 這些標識符的作用范圍是全局的(嚴格來說是 main() 函數內部),不能再定義與它們名字相同的變量。
2) Mon、Tues、Wed 等都是常量,不能對它們賦值,只能將它們的值賦給其他的變量。
枚舉和宏其實非常類似:宏在預處理階段將名字替換成對應的值,枚舉在編譯階段將名字替換成對應的值。我們可以將枚舉理解為編譯階段的宏。
總結:
(1) 枚舉型是一個集合,集合中的元素(枚舉成員)是一些命名的整型常量,元素之間用逗號,隔開。
(2)DAY是一個標識符,可以看成這個集合的名字,是一個可選項,即是可有可無的項。
(3) 第一個枚舉成員的默認值為整型的0,后續枚舉成員的值在前一個成員上加1。在當前值沒有賦值的情況下,枚舉類型的當前值總是前一個值+1.
(4) 可以人為設定枚舉成員的值,從而自定義某個范圍內的整數。
(5) 枚舉型是預處理指令#define的替代。
(6) 類型定義以分號;結束。

二、枚舉變量的定義
前面我們只是聲明了枚舉類型,接下來我們看看如何定義枚舉變量。
我們可以通過以下三種方式來定義枚舉變量
1、先定義枚舉類型,再定義枚舉變量
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
enum DAY day;
2、定義枚舉類型的同時定義枚舉變量
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;
3、省略枚舉名稱,直接定義枚舉變量
enum
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;
注意:同一個程序中不能定義同名的枚舉類型,不同的枚舉類型中也不能存在同名的命名常量。錯誤示例如下所示:
錯誤聲明一:存在同名的枚舉類型
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;

三、 使用枚舉類型的變量
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
}
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.使用枚舉型變量
#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

四、 枚舉類型的大小
#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
}
通過上面的實例,發現輸出都為4。實際上,枚舉類型在存儲時,是按照int型存儲的,在我的系統中也就是4字節方式存儲。
五、綜合舉例
#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
}



文章參考: https://www.cnblogs.com/JCSU/articles/1299051.html https://blog.csdn.net/weixin_42956373/article/details/112768376