`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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章