C enum (Enumeration)
Enumeration is a fundamental data type in the C language, which can make data more concise and easier to read.
The syntax for defining an enumeration is as follows:
enum EnumName {EnumElement1, EnumElement2, ...};
Let's take an example: There are 7 days in a week. If we don't use enumeration, we 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 quite verbose. Now let's see how it looks using enumeration:
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
Isn't this much cleaner?
Note: The default value of the first enumeration member is 0, and each subsequent member's value is incremented by 1 from the previous member. In this example, we set the first member's value to 1, so the second member is 2, and so on.
You can change the values of enumeration elements when defining the enumeration type:
enum season {spring, summer=3, autumn, winter};
Enumeration elements without specified values will have a value incremented by 1 from the previous element. So, spring is 0, summer is 3, autumn is 4, and winter is 5.
Definition of Enumeration Variables
So far, we have only declared the enumeration type. Now let's see how to define enumeration variables.
We can define enumeration variables in 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 type and the enumeration variable at the same time:
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;
3. Omit the enumeration name and directly define the enumeration variable:
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
, so according to the C specification, it is not possible to iterate over enumeration types.
However, under certain conditions where the enumeration type must be continuous, conditional iteration can be achieved.
The following example uses a for
loop to iterate over the elements of the 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:
Enumeration element: 1
Enumeration element: 2
Enumeration element: 3
Enumeration element: 4
Enumeration element: 5
Enumeration element: 6
Enumeration element: 7
The following enumeration type is not continuous, and thus cannot be iterated over:
enum
{
ENUM_0,
ENUM_10 = 10,
ENUM_11
};
Using enumeration in a switch
statement:
Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
enum color { red=1, green, blue };
enum color favorite_color;
/* User input to select a color */
printf("Please enter your favorite color: (1. red, 2. green, 3. blue): ");
scanf("%u", &favorite_color);
/* Output the 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 select a favorite color");
}
return 0;
}
The output of the above example is:
Please enter your favorite color: (1. red, 2. green, 3. blue): 1
Your favorite color is red
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; // Incorrect
printf("weekend:%d", weekend);
return 0;
}
The output of the above example is:
weekend:1