Detailed explanation of C language enumeration type enum and enumeration variables. enum function

enumeration definition

Keyword: enum
Purpose: Define an integer variable with a restricted value, used to limit the variable value range; a collection of macro definitions to define enumeration
variables:
enum{FALSE = 0, TRUE = 1} EnumName;
because of the enumeration variable The type is long, so typedef is usually used to change the variable type name
to reference the enumeration member:
EnumName = FALSE;
EnumName = TRUE;

Enumerate applications

Original link: https://blog.csdn.net/yetaodiao/article/details/127372017

enum is a keyword in the C language. enum is called an enumeration data type. The enumeration data type describes a set of integer values ​​(this sentence is actually not appropriate). The enumeration type is the preprocessing instruction #define Replacement, enumerations and macros are actually very similar. Macros replace names with corresponding values ​​in the preprocessing stage, and enumerations replace names with corresponding values ​​in the compilation stage.

We can understand enumerations as macros in the compilation phase, using the format:

enum typeName{
    
     valueName1, valueName2, valueName3, ...... };

is the name of the enumeration type. The elements (enumeration members) inside the curly braces are constants rather than variables. This must be clarified. Because the enumeration members are constants, they cannot be assigned values. They can only be assigned their values. For other variables,

when the enumeration type and the enumeration variable are defined together, the name of the enumeration type (that is, the week in enum week) can be omitted, which means that there are two ways of writing the above picture. The first one
  :

enum week{
    
    Mon=1, Tues, Wed, Thurs}num;

The second type:

enum {
    
    Mon=1, Tues, Wed, Thurs}num;

This is actually very similar to the nameless structure in the structure. Although defining enumeration type variables and defining structure type variables look very similar, the difference between the two is quite big. A structure type variable contains several members. Equivalent to a packed express box,

The enumeration type variable is different. The enumeration type variable is not a set containing several members. The enumeration type variable is actually similar to the int and char type variables. However, the assignment of the enumeration type variable can only use its own To assign values ​​to enumeration members, taking the above example,

The assignment of num can only use the enumeration members Mon, Tues, Wed, and Thurs, but not the enumeration members of other enumeration types.

Defining enumeration variables is the same as defining structure variables. There are two definition methods.

The first type: (define enumeration variables while defining the enumeration type)

enum week{
    
    Mon=1, Tues, Wed, Thurs}num;

The second type: (define the enumeration type first, then define the enumeration variable)

enum week{
    
    Mon=1, Tues, Wed, Thurs};
  enum week num;

Different examples will be used to illustrate the characteristics of enumeration types below.

Example 1: (Do not explicitly state the value of the enumeration constant)

enum week{Mon, Tues, Wed, Thurs, Fri, Sat, Sun};

Without explicit instructions, enumeration constants (that is, constant names in curly braces)The default value of the first enumeration constant is 0, each enumeration constant is incremented by 1 in turn, so Mon=0, Tues=1,・・・・Sun=6, let’s verify it below

enum week{
    
    Mon, Tues, Wed, Thurs, Fri, Sat, Sun};

  printf("Mon=%d

  Tues=%d

  Wed=%d

  Thurs=%d

  Fri=%d

  Sat=%d

  Sun=%d

  ", Mon, Tues, Wed, Thurs, Fri, Sat, Sun);

operation result:
  Insert image description here

——————————————
Copyright Statement: This article is an original article by CSDN blogger “Ye Tao Website Promotion Optimization” and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source for reprinting Links and this statement.

Example 2: (Display the value of the enumeration constant in the description)

enum week{
    
    Mon=1, Tues, Wed, Thurs, Fri, Sat, Sun};

In the above code, it is only explicitly stated that the value of the enumeration constant Mon is 1, and the value of the unspecified enumeration name will increase sequentially according to the last specified value (note that it is the last specified value)

enum week{
    
    Mon=1, Tues, Wed, Thurs, Fri, Sat, Sun};

  printf("Mon=%d

  Tues=%d

  Wed=%d

  Thurs=%d

  Fri=%d

  Sat=%d

  Sun=%d

  ", Mon, Tues, Wed, Thurs, Fri, Sat, Sun);

operation result:

Insert image description here

Modify the above code slightly

enum week{
    
    Mon=1, Tues, Wed, Thurs, Fri=10, Sat, Sun};
  printf("Mon=%d

  Tues=%d

  Wed=%d

  Thurs=%d

  Fri=%d

  Sat=%d

  Sun=%d
  ", Mon, Tues, Wed, Thurs, Fri, Sat, Sun);

operation result:
Insert image description here

Example 3: (Assigning values ​​to enumeration variables)

enum week{Mon=1, Tues, Wed, Thurs}num;

num =(enum week)10;

printf(“%d”, num);

An integer cannot be assigned to an enumeration variable. The enumeration type to which the enumeration variable belongs must be used for type coercion before assignment. In the above code, the coercion type conversion performed when assigning a value of 10 to the enumeration variable will change the entire value. The type constant 10 is converted into enum week type and then assigned to the num variable.

operation result:

Insert image description here

Summarize:

1. In the absence of display instructions, the default value of the enumeration constant (that is, the constant name in curly brackets) is 0 for the first enumeration constant, and each subsequent enumeration constant is incremented by 1.

2. In the case of partial display instructions, the value of the unspecified enumeration name will be incremented sequentially according to the last specified value.

3. An integer cannot be assigned to an enumeration variable. The enumeration type to which the enumeration variable belongs must be used for type coercion before assigning a value.

4. Different enumeration members in the same enumeration type can have the same value

5. Enumeration types with the same name cannot be defined in the same program, and enumeration members (enumeration constants) with the same name cannot exist in different enumeration types.

typedef enum week my;

or

typedef enum week{Mon=1, Tues, Wed, Thurs}my;

The above typedef can also omit week

typedef enum{Mon=1, Tues, Wed, Thurs}my;

In the above two forms, my is an alias of enum week (meaning that my and enum week are equivalent). It should be noted that if the typedef in the second sentence is removed, my will become a variable of enum week type. If you add If you go to typedef, then my is the alias of enum. You should pay attention to distinguish this point.

After defining the alias my of enum week, you can use my to define enumeration type variables later.

my num;

num =(enum week)10;

printf(“%d”, num);

enum function

enum eMovePriorityLevel {
    
    

eMovePriorityLevel_cant_move_unknown,

eMovePriorityLevel_cant_move_by_win, ///< 不能移动-已经胜利

eMovePriorityLevel_wall, ///< 不能移动-遇到了墙

eMovePriorityLevel_cant_move_by_go_twice, ///< 不能移动-已经走了2次

eMovePriorityLevel_can_move_by_go_once, ///< 可以移动-已经走了一次

eMovePriorityLevel_can_move_by_door, ///< 可以移动-是门, 从来没走过

};

使用枚举值类型的函数定义

enum eMovePriorityLevel GetMovePriorityLevel(char cPos)

使用枚举值类型的函数实现

enum eMovePriorityLevel GetMovePriorityLevel(char cPos)

{
    
    

enum eMovePriorityLevel Level;

switch (cPos)

{
    
    

case CHAR_WALL:

Level = eMovePriorityLevel_wall;

break;

case CHAR_DOOR:

Level = eMovePriorityLevel_can_move_by_door;

break;

case CHAR_EXIT:

Level = eMovePriorityLevel_cant_

Enum function 2

enum ICMDeviceType The name of the enumeration type,
Icmdevtype enumeration variable
Get_SensorType() enumeration function

1enum ICMDeviceType Icmdevtype = NOS;
2enum ICMDeviceType
{
    
    
     NOS=0, ICM20602, ICM42688, Adis16470,adis16800//枚举成员
};

3enum ICMDeviceType Get_SensorType()
{
    
    
    uint8_t whoami=0;
    read_1B( MPUREG_WHO_AM_I, &whoami );//读取ID,IIC,指针,形参地址
    if( whoami == 0x12 )
    {
    
    
        Icmdevtype = ICM20602;
    }
    enum ICMDeviceType Query_SensorType()
{
    
    
    return Icmdevtype;
}

Guess you like

Origin blog.csdn.net/weixin_44057803/article/details/132595743