enum in C

enum in C

Enumeration (enum) is a user-defined datatype (same as structure). It consists of various elements of that type. There is no such specific use of enum, we use it just to make our codes neat and more readable. We can write C programs without using enumerations also.
枚举 (enum) 是用户定义的数据类型 (与结构体相同)。它由该类型的各种元素组成。enum 没有具体的用途,我们只是为了使我们的代码更整洁,更易读。我们也可以不使用枚举就编写 C 程序。

For example, Summer, Spring, Winter and Autumn are the names of four seasons. Thus, we can say that these are of types season. Therefore, this becomes an enumeration with name season and Summer, Spring, Winter and Autumn as its elements.
例如,Summer, Spring, Winter and Autumn 是四个季节的名称。因此,可以说这些是季节类型。所以,这将成为一个以 season 命名,Summer, Spring, Winter and Autumn 为元素的枚举。

So, you are clear with the basic idea of enum. Now let’s see how to define it.
因此,您很清楚 enum 的基本思想。现在让我们看看如何定义它。

1. Defining an Enum

An enum is defined in the same way as structure with the keyword struct replaced by the keyword enum and the elements separated by comma as follows.
枚举的定义方式与结构体相同,用关键字 enum 代替关键字 struct,并用逗号分隔元素,如下所示。

enum enum_name
{
	element1,
	element2,
	element3,
	element4,
};

务必注意 union (联合) 和 struct (结构体) 里面的成员使用 ; (分号) 分割,而 enum (枚举) 里面的元素使用 , (逗号) 分割。

Now let’s define an enum of the above example of season.

enum season{
	Summer,
	Spring,
	Winter,
	Autumn
};

Here, we have defined an enum with name season and Summer, Spring, Winter and Autumn as its elements.

2. Declaration of Enum Variable

We also declare an enum variable in the same way as that of structures. We create an enum variable as follows.
我们也以与结构体相同的方式声明一个 enum 变量。我们如下创建一个 enum 变量。

enum season{
	Summer,
	Spring,
	Winter,
	Autumn
};

main()
{
	enum season s;
}

So, here s is the variable of the enum named season. This variable will represent a season. We can also declare an enum variable as follows.
因此,这里的 s 是名为 season 的枚举的变量。这个变量代表一个季节。我们还可以如下声明一个枚举变量。

enum season
{
	Summer, 
	Spring, 
	Winter, 
	Autumn
} s;

3. Values of the Members of Enum

All the elements of an enum have a value. By default, the value of the first element is 0, that of the second element is 1 and so on.
枚举的所有元素都有一个值。默认情况下,第一个元素的值为 0,第二个元素的值为 1,依此类推。

在这里插入图片描述

Let’s see an example.

//============================================================================
// Name        : enum
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>
enum season
{
	Summer, Spring, Winter, Autumn
};

int main()
{
	enum season s;
	s = Spring;
	printf("%d\n", s);

	s = Summer;
	printf("%d\n", s);

	s = Winter;
	printf("%d\n", s);

	s = Autumn;
	printf("%d\n", s);

	return 0;
}

Output

1
0
2
3

Here, first we defined an enum named season and declared its variable s in the main function as we have seen before. The values of Summer, Spring, Winter and Autumn are 0, 1, 2 and 3 respectively. So, by writing s = Spring, we assigned a value 1 to the variable s since the value of Spring is 1.
在这里,我们首先定义了一个名为 season 的枚举,并在主函数中声明了它的变量 s,如我们之前所见。Summer, Spring, Winter and Autumn 的值分别为 0、1、2 和 3。因此,通过写 s = Spring,我们将变量 s 赋值为 1,因为 Spring 的值为 1。

We can also change the default value and assign any value of our choice to an element of enum. Once we change the default value of any enum element, then the values of all the elements after it will also be changed accordingly. An example will make this point clearer.
我们还可以更改默认值,并将我们选择的任何值分配给枚举元素。一旦我们更改了任何枚举元素的默认值,那么其后所有元素的值也将相应地更改。一个例子将使这一点更清楚。

//============================================================================
// Name        : enum
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>
enum days
{
	sun, mon, tue = 5, wed, thurs, fri, sat
};

int main()
{
	enum days day;

	day = sun;
	printf("%d\n", day);

	day = mon;
	printf("%d\n", day);

	day = tue;
	printf("%d\n", day);

	day = wed;
	printf("%d\n", day);

	day = thurs;
	printf("%d\n", day);

	day = fri;
	printf("%d\n", day);

	day = sat;
	printf("%d\n", day);

	return 0;
}

Output

0
1
5
6
7
8
9

The default value of sun will be 0, mon will be 1, tue will be 2 and so on. In the above example, we defined the value of tue as 5. So the values of wed, thurs, fri and sat will become 6, 7, 8 and 9 respectively. There will be no effect on the values of sun and mon which will remain 0 and 1 respectively. Thus the value of thurs i.e. 7 will get printed.
sun 的默认值为 0,mon 的默认值为 1,tue 的默认值为 2,依此类推。在上面的示例中,我们将 tue 的值定义为 5。因此,wed, thurs, fri and sat 的值将分别变为 6、7、8 和 9。不会影响分别为 0 和 1的 sun and mon 的值。这样,thurs 的值即 7 将被打印出来。

Let’s see one more example of enum.

//============================================================================
// Name        : enum
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <stdio.h>
enum days
{
	sun, mon, tue, wed, thurs, fri, sat
};

int main()
{
	enum days day;
	day = thurs;
	printf("%d\n", day);

	day = thurs;
	printf("%d\n", day + 2);

	return 0;
}

Output

4
6

In this example, the value of thurs i.e. 4 is assigned to the variable day. Since we are printing day+2 i.e. 6 (=4+2), so the output will be 6.
在这个例子中,thurs 的值即 4 被分配给变量 day。由于我们正在打印 day + 2,即 6 (=4+2),因此输出为 6。

发布了509 篇原创文章 · 获赞 1824 · 访问量 110万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104782067
今日推荐