Enumeration (enum) links a string of names with a string of reshaping values in a simple way. Can replace the #define Declaration
Enumerate in general form:
Enum Optional label {content ...} Optional definition variable definition;
Where "content ..." is a list of identifiers and may have some integer values assigned to them.
Enum asn{mon=7, TUE, wed=10, THU, FRI};
1.ASN is an identifier that can be viewed as the name of the set, an optional, or optional item.
2. By default, integer values start at 0. If an identifier in the list is assigned, then the value of that identifier immediately after is greater than the assigned value, and then the analogy.
3. End With;
An enum can access an enumerated value through an enumerated element by using an enum name dot. Cases:
int i = (int) ASN. MON enumerated values are of type int, but must also be cast for display.
You can assign an enumeration value only to a variable, not directly to an enumeration variable, unless the cast
Enum Day
{
Mon=1, TUE, WED, THU, FRI, SAT, SUN
};
Enum Day Good_day, Bad_day; Variable good_day and Bad_day types are enumerated enum day
Enum//Is different from the first definition, the label day here is omitted, which is allowed.
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
} workday; The type of variable workday is enum type enum day
Enum BOOLEAN {false, true} End_flag, Match_flag; Defines an enumeration type and declares two enumerated variables
typedef enum Usage
typedef enum WORKDAY
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
};
Workday today, tomorrow; The types of variables today and tomorrow are enumerated workday, that is, enum workday
Note: Enumeration types with the same name cannot be defined in the same program, and named constants with the same name cannot exist in different enumeration types.
Advantages: #define定义的名字在编译时被丢弃, while enumerated names are usually visible in the debugger and can be used when debugging code.
Program Examples:
Ride
Output 0,1,2