First, the concept of enumerations
Enumerations are a basic data type in the C language, not a constructed type, which can be used to declare a set of constants. When a variable has several fixed possible values, the variable can be defined as an enumeration type. For example, you can use a variable of an enumeration type to represent the season, because there are only 4 possible values for the season: Spring, summer, autumn, and winter.
1. The general form is: Enum enumeration Name {Enumeration element 1, Enumeration element 2,......};
For example:
Enum Season {
Spring
Summer
Autumn
Winter
};
Second, the use of attention
The 1> C language compiler handles enumeration elements (spring, summer, and so on) As Integer constants, called enumeration constants.
The value of the 2> enumeration element depends on the order in which the enumeration elements are arranged when defined. By default, the first enumeration element has a value of 0, the second is 1, and sequentially adds 1.
Enum Season {Spring, summer, autumn, winter};
That is, spring has a value of 0,summer value of 1,autumn and a value of 2,winter is 3
3> can also change the value of an enumeration element when defining an enumeration type
Enum season {spring, summer=3, Autumn, winter};
An enumeration element with no value specified, with a value of 1 for the previous element. It is also said that the value of spring is 0,summer value of 3,autumn is 4,winter value is 5
1#include <stdio.h>2 3 intMain ()4 {5 enumSex {man, Woman, unkown};6 7 //0 Male 1 female-1 unspecified8 //int sex = 3;9 //enum Sex s = unkown;Ten One //1. Defining Enum Types A enumSeason - { -Spring =1, the Summer, - Autumn, - Winter - }; + - //2. Defining enumeration Variables + enumSeason s =100000; A at -printf"%d\n", s); - - - return 0; -}
"Learning note", "C language" enumeration