C语言中的枚举(或枚举)

Enumeration(或enum)是C语言中一种用户定义的数据类型。它主要用于为整型常量指定名称,这些名称使程序易于阅读和维护。

null

图片[1]-C语言中的枚举(或枚举)-yiteyi-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, ....... };

也可以定义enum类型的变量。它们可以用两种方式定义:

// 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程序中,星期天的值为0,星期一的值为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程序的输出

项目1:

#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;
}


项目2:

#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;
}


枚举与宏 我们还可以使用宏来定义名称和常量。例如,我们可以使用以下宏定义“工作”和“失败”。

#define Working 0
#define Failed 1
#define Freezed 2


当许多相关的命名常量具有整数值时,使用enum比使用宏有多个优点。 a) 枚举遵循范围规则。 b) 枚举变量是自动赋值的。以下内容更简单

enum state  {Working, Failed, Freezed};


如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

本文的引言部分由 皮尤什·瓦希斯塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享