Enumeration (enum) in C language

Enumeration is a basic data type in C language used to define a set of constants with discrete values. It can make data more concise and easier to read.

Enumeration types are usually used to name a group of related constants in a program to facilitate the readability and maintainability of the program.

To define an enumeration type, you need to 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 an integer value can be assigned to them. If not specified, then the default is incremented from 0.

The enumeration syntax definition format is:

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

Next, let's take an example, for 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 seems to be a lot of code. Next, let's look at the way to use enumeration:

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

Doesn't it look more concise?

Note: The default value of the first enumeration member is an integer of 0, and the value of subsequent enumeration members is added to the previous member by 1. In this example, we define the value of the first enumeration member as 1, the second as 2, and so on.

You can change the value of an enumeration element when defining an enumeration type:

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

An enumeration element without a specified value has the value of the previous element plus 1. In other words, the value of spring is 0, the value of summer is 3, the value of autumn is 4, and the value of winter is 5

Definition of enumeration variables

We just declared the enumeration type earlier, and then we look at how to define the enumeration variable.

We can define enumeration variables in the following three ways

1. Define the enumeration type first, and then define the enumeration variable

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

2. Define the enumeration variable while defining the enumeration type

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 language, the enumeration type is treated as int or unsigned int type, so according to the C language specification, there is no way to traverse the enumeration type.

However, in some special cases, enumeration types must be continuous so that conditional traversal 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()
{
    // 遍历枚举元素
    for (day = MON; day <= SUN; day++) {
        printf("枚举元素:%d \n", day);
    }
}

The output of the above example is:

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

The following enumeration types are not contiguous, and such enumerations cannot be traversed.

enum
{
    ENUM_0,
    ENUM_10 = 10,
    ENUM_11
};

Convert an integer to an enum

The following example converts an integer to an enum:

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;  //类型转换
    //weekend = a; //错误
    printf("weekend:%d",weekend);
    return 0;
}

The output of the above example is: weekend:1

Guess you like

Origin blog.csdn.net/weixin_55735677/article/details/130602941