A detailed explanation of enumeration types in C language

Keywords: Programming

Scene entry

In programming, some data values are often limited, only a very small number of integers, and it is better to take a name for each value to facilitate the use in the subsequent code, such as a week only seven days, a year only twelve months, a class has six courses a week, etc.

For example, seven days a week, we can use the เท define command to assign a name to each day:

#include <stdio.h>

#define Mon 1
#define Tues 2
#define Wed 3
#define Thurs 4
#define Fri 5
#define Sat 6
#define Sun 7

int main(){
    int day;
    scanf("%d", &day);
    switch(day){
        case Mon: puts("Monday"); break;
        case Tues: puts("Tuesday"); break;
        case Wed: puts("Wednesday"); break;
        case Thurs: puts("Thursday"); break;
        case Fri: puts("Friday"); break;
        case Sat: puts("Saturday"); break;
        case Sun: puts("Sunday"); break;
        default: puts("Error!");
    }
    return 0;
}

Operation result:

5โ†™
Friday

#Although the define command can solve the problem, it also brings a lot of side effects, resulting in too many macro names and loose code, which always seems a little uncomfortable. C provides an Enum type that lists all possible values and gives them a name.

Enumeration types are defined as:

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

enum is a new keyword specially used to define enumeration type; typeName is the name of enumeration type; valueName1, valueName2, valueName3 Is a list of names for each value. Pay attention to the last; not least.

For example, list the days of the week:

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

As you can see, we only give the name, but not the value corresponding to the name. This is because the enumeration value starts from 0 by default, and then adds 1 (increment) one by one. That is to say, Mon and Tues in week The Sun values are 0, 1 6.

We can also specify a value for each name:

enum week{ Mon = 1, Tues = 2, Wed = 3, Thurs = 4, Fri = 5, Sat = 6, Sun = 7 };

A simpler way is to specify a value for only the first name:

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

In this way, the enumeration value is incremented from 1, which is equivalent to the above writing method.

Enumeration is a type that allows you to define enumeration variables:

enum week a, b, c;

You can also define variables while defining enumeration types:

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

With enumeration variables, you can assign values in the list to it:

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

Or:

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

The example determines the day of the week entered by the user.

#include <stdio.h>

int main(){
    enum week{ Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun } day;
    scanf("%d", &day);
    switch(day){
        case Mon: puts("Monday"); break;
        case Tues: puts("Tuesday"); break;
        case Wed: puts("Wednesday"); break;
        case Thurs: puts("Thursday"); break;
        case Fri: puts("Friday"); break;
        case Sat: puts("Saturday"); break;
        case Sun: puts("Sunday"); break;
        default: puts("Error!");
    }
    return 0;
}

Operation result:

4โ†™
Thursday

Particular attention
1. The identifiers Mon, Tues, and Wed in the enumeration list have a global scope (strictly speaking, within the main() function), and can no longer define variables with the same name.

2. Mon, Tues, Wed, etc. are constants. They cannot be assigned values, but their values can only be assigned to other variables.

Enumerations are very similar to macros: macros replace names with corresponding values in preprocessing and enumerations replace names with corresponding values in compilation. We can think of enumerations as macros at compile time.

For the above code, it will become similar to the following at some time of compilation:

#include <stdio.h>

int main(){
    enum week{ Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun } day;
    scanf("%d", &day);
    switch(day){
        case 1: puts("Monday"); break;
        case 2: puts("Tuesday"); break;
        case 3: puts("Wednesday"); break;
        case 4: puts("Thursday"); break;
        case 5: puts("Friday"); break;
        case 6: puts("Saturday"); break;
        case 7: puts("Sunday"); break;
        default: puts("Error!");
    }
    return 0;
}

The names Mon, Tues, and Wed are all replaced with corresponding numbers. This means that Mon, Tues, and Wed are not variables. They do not occupy the memory of the data area (constant area, global data area, stack area and heap area), but are directly compiled into the command and put into the code area, so they cannot be used to obtain their addresses. This is the essence of enumeration.

Enumeration type variables need to store an integer, and its length and int should be the same. Let's verify:

#include <stdio.h>

int main(){
    enum week{ Mon = 1, Tues, Wed, Thurs, Fri, Sat, Sun } day = Mon;
    printf("%d, %d, %d, %d, %d\n", sizeof(enum week), sizeof(day), sizeof(Mon), sizeof(Wed), sizeof(int) );
    return 0;
}

Operation result:

4, 4, 4, 4, 4
352 original articles published, 91 praised, 10000 visitors+
Private letter follow

Posted by toxic_brain on Wed, 26 Feb 2020 19:00:59 -0800