C enum (Enumeration)

Enumeration is a basic data type in C, used to define a set of constants with discrete values. It makes data more concise and readable.

Enumeration types are typically used to assign names to a group of related constants in a program, making the program more readable and maintainable.

To define an enumeration type, you need to useenumThe keyword, followed by the name of the enumeration type, and using braces{}a set of enumeration constants enclosed. Each enumeration constant can be represented by an identifier, and you can also specify an integer value for them. If not specified, by default from0Starts incrementing.

The enum syntax definition format is:

enum 枚举名 {枚举元素1,枚举元素2,……};

Next, let's take an example: a week has 7 days. If we don't use enums, 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 like quite a lot of code. Next, let's look at using enums:

enum DAY
{
      MON=1, TUE, WED, THU, FRI, SAT, SUN
};

Doesn't this look more concise?

Note:The default value of the first enumeration member is the integer 0, and the value of each subsequent enumeration member is the previous member's value plus 1. In this example, we define the first enumeration member's value as 1, the second as 2, and so on.

You can change the values of enum elements when defining the enum type:

enum season {spring, summer=3, autumn, winter};

Enum elements without a specified value have the value of the previous element plus 1. That is, spring is 0, summer is 3, autumn is 4, winter is 5

Definition of enumeration variables

Earlier we only declared the enum type. Next, let's see how to define enum variables.

We can define enum variables in the following three ways

1. Define the enum type first, then define the enum variable

enum DAY
{
      MON=1, TUE, WED, THU, FRI, SAT, SUN
};
enum DAY day;

2. Define the enum variable while defining the enum type

enum DAY
{
      MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;

3. Omit the enum name and directly define the enum 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 result of the above example is:

3

In C, enum types are treated as int or unsigned int types, so according to the C language specification, there is no way to traverse an enum type.

However, in some special cases, the enum type must be continuous, which allows conditional traversal.

The following example uses a for loop to iterate through the enum elements:

Example

#include <stdio.h> enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN } day; int main() { // Traverse enum elements for (day = MON; day <= SUN; day++) { printf("Enum element: %d\n", day); } }

The output result of the above example is:

枚举元素:1 
枚举元素:2 
枚举元素:3 
枚举元素:4 
枚举元素:5 
枚举元素:6 
枚举元素:7

The following enum type is not continuous, and this kind of enum cannot be traversed.

enum
{
    ENUM_0,
    ENUM_10 = 10,
    ENUM_11
};

Using enums in switch:

Example

#include <stdio.h> #include <stdlib.h> int main() { enum color { red=1, green, blue }; enum color favorite_color; /*User inputs a number to select 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("The color you like 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 result of the above example is:

请输入你喜欢的颜色: (1. red, 2. green, 3. blue): 1
你喜欢的颜色是红色

Converting an integer to an enum

The following example converts integers to enumerations:

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 result of the above example is:

weekend:1
other extensions