SoFunction
Updated on 2025-03-04

Case explanation of the usage of enum type in C language

11.10 Enumeration Type

In practical problems, the values ​​of some variables are limited to a limited range. For example, there are only seven days in a week, only twelve months in a year, and a class has six courses per week, and so on. If these quantities are described as integers, it is obviously inappropriate to character types or other types. To this end, C language provides a type called "enumeration". All possible values ​​are listed in the definition of the "enum" type, and the variable values ​​of the "enum" type cannot exceed the defined range.

It should be noted that an enum type is a primitive data type, not a construct type, because it cannot be decomposed into any primitive type anymore.

11.10.1 Enumeration type definition and enumeration variable description

1. Enumeration definition

The general form of enumeration type definition is:

enum enum name { enum value table };

All available values ​​should be listed in the enumeration value table. These values ​​are also called enumeration elements.

For example:

The enum is called weekday, and there are 7 enum values, which is seven days of the week. Any value that is described as a weekday type variable can only be one day of seven days.

2. Description of enumeration variables

Just like structures and unions, enumeration variables can also be explained in different ways, that is, they can be defined first and then explained, and they can be defined or explained directly.

If the variables a, b, and c are specified as the weekdays mentioned above, any of the following methods may be adopted:

enum weekday{ sun,mou,tue,wed,thu,fri,sat };

enum weekday a,b,c;

Or: enum weekday{ sun,mou,tue,wed,thu,fri,sat }a,b,c;

Or: enum { sun,mou,tue,wed,thu,fri,sat }a,b,c;

11.10.2 Assignment and use of enumerated type variables

The enumeration type has the following provisions during use:

1. Enumeration values ​​are constants, not variables. You cannot use assignment statements to assign values ​​to it in a program. For example, the following assignment is made to the elements that enumerate weekday: sun=5; mon=2; sun=mon; are all wrong.

2. The enumeration element itself defines a numeric value representing the sequence number by the system, and the order starting from 0 is defined as 0, 1, 2…. For example, in weekday, the sun value is 0, the mon value is 1, and the sat value is 6.

【Example 11.10】

main(){
enum weekday
{ sun,mon,tue,wed,thu,fri,sat } a,b,c;
a=sun;
b=mon;
c=tue;
printf("%d,%d,%d",a,b,c);
}

Note: You can only assign enum values ​​to enum variables, and you cannot directly assign element values ​​to enum variables. like:

a=sum;

b=mon;

It's correct. and:

a=0;

b=1;

It's wrong. If you must assign a numeric value to an enumeration variable, you must use a cast. like:

a=(enum weekday)2;

Its meaning is to assign an enum element with the order number 2 to the enum variable a, which is equivalent to:

a=tue; It should also be noted that the enumeration element is not a character constant or a string constant. Do not add single or double quotes when using it.

【Example 11.11】

main(){
enum body
{ a,b,c,d } month[31],j;
int i;
j=a;
for(i=1;i<=30;i++){
month[i]=j;
j++;
if (j>d) j=a;
}
for(i=1;i<=30;i++){
switch(month[i])
{
case a:printf(" %2d %c\t",i,'a'); break;
case b:printf(" %2d %c\t",i,'b'); break;
case c:printf(" %2d %c\t",i,'c'); break;
case d:printf(" %2d %c\t",i,'d'); break;
default:break;
}
}
printf("\n");
}

This is the article about the case study on the usage of enum types in C language. For more relevant content on the usage of enum types in C language, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!