The usage of enum in C language

Posted by imi_99 on Tue, 22 Feb 2022 23:09:34 +0100

brief introduction

The definition format of enumeration syntax is:

enum Enumeration name {Enumeration element 1,Enumeration element 2,......};

Let's take an example. For example, there are 7 days in a week. If you don't need to enumerate, you 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

It seems that there is a large amount of code. Next, let's look at the way to use enumeration:

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

Does this look more concise.

Note: the default value of the first enumeration member is integer 0, and the value of subsequent enumeration members is added to the previous member by 1. In this example, the value of the first enumeration member is defined as 1, the second is 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};

Enumeration elements that do not have a specified value have a 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. Next, let's see how to define the enumeration variable.

Enumeration variables can be defined in three ways

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

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

2. Define enumeration variables while defining enumeration types

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

3

Enumeration traversal

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

However, in some special cases, enumeration types must be continuous, which can realize conditional traversal.

The following example uses for to traverse enumerated elements:

example

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

The output result of the above example is:

Enumeration element: 1 
Enumeration element: 2 
Enumeration elements: 3 
Enumeration elements: 4 
Enumeration elements: 5 
Enumeration elements: 6 
Enumeration elements: 7

The following enumeration types are discontinuous and cannot be traversed.

enum
{
    ENUM_0,
    ENUM_10 = 10,
    ENUM_11
};

Use of enumeration in switch

example

#include <stdio.h>
#include <stdlib.h>
int main()
{
 
    enum color { red=1, green, blue };
 
    enum  color favorite_color;
 
    /* The user enters a number to select a color */
    printf("Please enter your favorite color: (1. red, 2. green, 3. blue): ");
    scanf("%u", &favorite_color);
 
    /* Output results */
    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 didn't choose the color you like");
    }
 
    return 0;
}

The output result of the above example is:

Please enter your favorite color: (1. red, 2. green, 3. blue): 1
 Your favorite color is red

Converts an integer to an enumeration

The following example converts an integer to an enumeration:

#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

Topics: C