In practical problems, the values of some variables are limited in a limited range. For example, there are only seven days in a week, only twelve months in a year, and a class has six classes a week. It is obviously inappropriate to describe these quantities as integers, characters or other types. To this end, the C language provides a type called "enumeration". All possible values are listed in the definition of "enumeration" type, and the values of variables described as "enumeration" type cannot exceed the defined range. Note that the enumeration type is a basic data type, not a constructed type, because it cannot be decomposed into any basic types.
11.10.1Definition of enumeration types and description of enumeration variables.
Definition of 1. Enumeration The general form of enumeration type definition is:
Enumeration Enumeration Name {Enumeration Value Table};
All available values should be listed in the list of enumerated values. These values are also called enumeration elements.
For example:
The enumeration is named weekday and has seven enumeration values * * *, that is, seven days in a week. Variable values described as working days can only be one of seven days.
2. Description of enumeration variables
Like structure and union, enumeration variables can be described in different ways, that is, they can be defined first and then described, or they can be defined or directly described.
Variables A, B and C are described as the above working days, and any of the following methods can be adopted:
List working days (Sunday; Monday; Tuesday; Wednesday; Thursday; Friday and Saturday).
Enumerate working days a, b and c;
Or for:
List working days (Sunday; Monday; Tuesday; Wednesday; Thursday; Friday and Saturday) a; b and c;
Or for:
List {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }a, B, C;
1 1. 10.2 enumerates the assignment and use of type variables.
Enumeration types have the following provisions in use:
The 1. enumeration value is constant, not variable. You can't assign a value to it in a program with an assignment statement.
For example, assign the following values to the elements enumerating weekday:
Sun = 5;
mon = 2;
sun = mon
It's all wrong
2. Enumeration elements themselves are defined by the system with a numerical value representing serial number, starting from 0 and defined as 0, 1, 2… and so on. For example, on weekdays, the sun value is 0, the mon value is 1, …, and the sat value is 6.
Example 1 1. 10
main(){
Enumerating working days
{Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday} a, B, C;
A = sun;
b = mon
c = tue
printf("%d,%d,%d ",a,b,c);
}
Description:
Only enumeration values can be assigned to enumeration variables, and the numerical values of elements cannot be directly assigned to enumeration variables. For example:
A = sum;
b = mon
Is correct. And:
a = 0;
b = 1;
Is wrong. If you must assign a value to an enumeration variable, you must use cast.
For example:
a =(enum weekday)2;
Its significance lies in giving enumeration element with serial number 2 to enumeration variable A, which is equivalent to:
a = tue
It should also be noted that enumeration elements are not character constants or string constants, so do not add single or double quotes when using them.
Example11.11
main(){
Enumeration body
{a, b, c, d} month [3 1], j;
int I;
j = a;
for(I = 1; I & lt=30; i++){
month[I]= j;
j++;
if(j & gt; d)j = a;
}
for(I = 1; I & lt=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 ");
}