Some things about enumeration types

The content of today’s chapter includes the definition, usage and advantages of enumeration types;

enum definition

The definition of enumeration is to list the possible values ​​one by one. For example, in our real life: Monday to Sunday are a limited number of 7 days in a week, and they can be listed one by one.

enum type definition

enum Color//颜色类型
{
    RED,
    GREEN,
    BLUE
};

int main()
{
    enum Color c = BLUE;
    
    return 0;
}

In the enumeration type, the possible values ​​of the enumeration type are placed in the curly brackets, which are constants; and in the structure, the members of the structure are placed.

The constants in the enumeration type are incremented by one in sequence. When defining the enumeration type, you can assign initial values ​​to the constants in the enumeration type. So you can change the number when assigning an initial value to a constant.

 Advantages of Enums

1. Increase the readability and maintainability of the code;

2. Compared with identifiers defined by #define, enumeration types have type checking, which is more rigorous;

3. Prevents naming pollution (encapsulation);

4. Easy to debug;

5. Easy to use, multiple variables can be defined at one time.

In general, an enumeration type is a type whose members are integer types.

Guess you like

Origin blog.csdn.net/2301_77868664/article/details/130665445