吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → C中的枚举

C中的枚举

来源:网络 | 更新时间:2022-01-19 07:59:02
枚举是 C 语言中用户定义的数据类型。它主要用于为整数常量分配名称,这些名称使程序易于阅读和维护。
enum State {Working = 1, Failed = 0}; 
关键字“enum”用于在 C 和 C++ 中声明新的枚举类型。以下是枚举声明的示例。
// The name of enumeration is "flag" and the constant
// are the values of the flag. By default, the values
// of the constants are as follows:
// constant1 = 0, constant2 = 1, constant3 = 2 and 
// so on.
enum flag{constant1, constant2, constant3, ....... };
也可以定义枚举类型的变量。它们可以通过两种方式定义:
// In both of the below cases, "day" is 
// defined as the variable of type week. 
enum week{Mon, Tue, Wed};
enum week day;
// Or
enum week{Mon, Tue, Wed}day;
// An example program to demonstrate working
// of enum in C
#include<stdio.h>

enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

int main()
{
enum week day;
day = Wed;
printf("%d",day);
return 0;
} 
输出:
2
在上面的示例中,我们将“day”声明为变量,并将“Wed”的值分配给 day,即 2。因此,打印了 2。 枚举的另一个例子是:
// Another example program to demonstrate working
// of enum in C
#include<stdio.h>

enum year{Jan, Feb, Mar, Apr, May, Jun, Jul, 
Aug, Sep, Oct, Nov, Dec};

int main()
{
int i;
for (i=Jan; i<=Dec; i++) 
printf("%d ", i);

return 0;
}
输出:
0 1 2 3 4 5 6 7 8 9 10 11
在此示例中,for 循环将从 i = 0 运行到 i = 11,因为最初 i 的值是 Jan,即 0,Dec 的值是 11。 关于枚举初始化的有趣事实。 1.两个枚举名称可以具有相同的值。例如,在以下 C 程序中,“失败”和“冻结”都具有相同的值 0。
#include <stdio.h>
enum State {Working = 1, Failed = 0, Freezed = 0};

int main()
{
printf("%d, %d, %d", Working, Failed, Freezed);
return 0;
}
输出:
1, 0, 0
2.如果我们没有显式地为枚举名赋值,编译器默认从0开始赋值。例如,在下面的C程序中,sunday取值0,monday取1,以此类推。
#include <stdio.h>
enum day {sunday, monday, tuesday, wednesday, thursday, friday, saturday};

int main()
{
enum day d = thursday;
printf("The day number stored in d is %d", d);
return 0;
}
输出:
The day number stored in d is 4
3.我们可以按任意顺序为某个名称赋值。所有未分配的名称都将值作为前一个名称的值加一。
#include <stdio.h>
enum day {sunday = 1, monday, tuesday = 5,
wednesday, thursday = 10, friday, saturday};

int main()
{
printf("%d %d %d %d %d %d %d", sunday, monday, tuesday,
wednesday, thursday, friday, saturday);
return 0;
}
输出:
1 2 5 6 10 11 12
4.分配给枚举名称的值必须是某个整数常量,即该值必须在从最小可能整数值到最大可能整数值的范围内。 5.所有枚举常量在其范围内必须是唯一的。例如,以下程序编译失败。
enum state {working, failed};
enum result {failed, passed};

int main() { return 0; }
输出:
Compile Error: 'failed' has a previous declaration as 'state failed'
练习: 预测以下 C 程序的输出 方案一:
#include <stdio.h>
enum day {sunday = 1, tuesday, wednesday, thursday, friday, saturday};

int main()
{
enum day d = thursday;
printf("The day number stored in d is %d", d);
return 0;
}
方案二:
#include <stdio.h>
enum State {WORKING = 0, FAILED, FREEZED};
enum State currState = 2;

enum State FindState() {
return currState;
}

int main() {
(FindState() == WORKING)? printf("WORKING"): printf("NOT WORKING");
return 0;
}
枚举 vs 宏 我们也可以使用宏来定义名称常量。例如,我们可以使用以下宏定义“工作”和“失败”。
#define Working 0
#define Failed 1
#define Freezed 2
当许多相关的命名常量具有整数值时,使用枚举优于宏有多个优点。 a) 枚举遵循范围规则。 b) 枚举变量被自动赋值。以下更简单
enum state {Working, Failed, Freezed};
 

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com