C enum (Enumeration)
Enumeration is a basic data type in C used to define a set of constants with discrete values; it can make data more concise and more readable.
Enumeration types are commonly used to name a group of related constants in a program, improving readability and maintainability.
To define an enumeration type, you use the enum keyword followed by the name of the enumeration type and a set of enumeration constants enclosed in braces {}. Each enumeration constant can be represented by an identifier, or you can assign an integer value to it; if not specified, the value starts at 0 and increases by default.
The syntax definition format of an enumeration is:
enum 枚举名 {枚举元素1,枚举元素2,……};
Next let us give an example: a week has 7 days. Without enumeration, we would need to use #define to define an alias for each integer:
#define
MON
1
#define
TUE
2
#define
WED
3
#define
THU
4
#define
FRI
5
#define
SAT
6
#define
SUN
7
This looks like a lot of code. Next let us see the way using enumeration:
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
Does this look more concise?
**Note:** The default value of the first enumeration member is the integer 0, and each subsequent member's value is the previous member's value plus 1. In this example we define the first member's value as 1, the second as 2, and so on.
You can change the values of enumeration elements when defining the enumeration type:
enum season {spring, summer=3, autumn, winter};An enumeration element with no specified value takes the previous element's value plus 1. That is, spring has the value 0, summer has 3, autumn has 4, and winter has 5
Definition of enumeration variables
Earlier we only declared the enumeration type; next let us see how to define enumeration variables.
We can define enumeration variables in the following three ways
1. Define the enumeration type first, then define the enumeration variable
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
enum DAY day;
2. Define the enumeration variable at the same time as the enumeration type
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;
3. Omit the enumeration name and define the enumeration variable directly
enum
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;
Example
#include
<
stdio.h
enum
DAY
{
MON
=
1
,
TUE
,
WED
,
THU
,
FRI
,
SAT
,
SUN
};
int
main
(
)
{
enum
DAY
day
;
day
=
WED
;
printf
(
"
%d
"
,
day
)
;
return
0
;
}
The output of the above example is:
3
In C, enumeration types are treated as int or unsigned int types, so according to the C language specification there is no way to iterate over an enumeration type.
However, in some special cases, if the enumeration values are consecutive, conditional iteration can be achieved.
The following example uses for to iterate over the elements of an enumeration:
Example
#include
<
stdio.h
enum
DAY
{
MON
=
1
,
TUE
,
WED
,
THU
,
FRI
,
SAT
,
SUN
}
day;
int
main
(
)
{
// Iterate over enumeration elements
for
(
day
=
MON
;
day
<=
SUN
;
day
++
)
{
printf
(
"
Enumeration element: %d
\
n
"
,
day
)
;
}
}
The output of the above example is:
枚举元素:1
枚举元素:2
枚举元素:3
枚举元素:4
枚举元素:5
枚举元素:6
枚举元素:7
The following enumeration type is not consecutive, so it cannot be iterated.
enum
{
ENUM_0,
ENUM_10 = 10,
ENUM_11
};
Using enumeration in switch:
Example
#include
<
stdio.h
#include
<
stdlib.h
int
main
(
)
{
enum
color
{
red
=
1
,
green
,
blue
};
enum
color
favorite_color
;
/*
The user enters a number to choose a color
*/
printf
(
"
Please enter your favorite color: (1. red, 2. green, 3. blue):
"
)
;
scanf
(
"
%u
"
, &
favorite_color
)
;
/*
Output result
*/
switch
(
favorite_color
)
{
case
red
:
printf
(
"
Your favorite color is red
"
)
;
break
;
case
green
:
printf
(
"
Your favorite color is green
"
)
;
break
;
case
blue
:
printf
(
"
Your favorite color is blue
"
)
;
break
;
default
:
printf
(
"
You did not choose your favorite color
"
)
;
}
return
0
;
}
The output of the above example is:
请输入你喜欢的颜色: (1. red, 2. green, 3. blue): 1
你喜欢的颜色是红色
Converting an integer to an enumeration
The following example converts an integer to an enumeration:
Example
#include
<
stdio.h
#include
<
stdlib.h
int
main
(
)
{
enum
day
{
saturday
,
sunday
,
monday
,
tuesday
,
wednesday
,
thursday
,
friday
}
workday;
int
a
=
1
;
enum
day
weekend
;
weekend
=
(
enum
day
)
a
;
// Type conversion
//weekend = a; // Error
printf
(
"
weekend:%d
"
,
weekend
)
;
return
0
;
}
The output of the above example is:
weekend:1