1. Enumeration definition the general form of Enumeration type definition is: Enum enumeration name {enumeration value table }; All available values should be listed in the enumerated values table. These values are also called enumeration elements. For example: The enumerated name is weekday. There are 7 enumerated values, that is, seven days in a week. The value of a variable described as weekday can only be one day in seven days. 2. Description of enumerated Variables Just like the structure and union, enumeration variables can be described in different ways, that is, they are defined first and then described at the same time. The variables A, B, and C are described as the preceding weekday. You can use either of the following methods: 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; The following rules apply to enumeration types: 1. The enumerated value is a constant, not a variable. You cannotProgramUse the value assignment statement to assign values to it. For example, assign the following values to the elements that enumerate weekday: Sun = 5; MON = 2; Sun = mon; All are errors. 2. The enumeration element itself is defined by the system as a numerical value indicating the serial number. The order starting from 0 is defined as 0, 1, 2 .... For example, in weekday, Sun is 0, mon is 1 ,..., 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: Only enumeration values can be assigned to enumeration variables, and the element values cannot be directly assigned to enumeration variables. For example: A = sum; B = mon; Is correct. And: A = 0; B = 1; Yes. If you want to assign a value to an enumerated variable, you must use forced type conversion. For example: A = (Enum weekday) 2; In this way, the enumerated element with the ordinal number 2 is assigned to the enumerated variable A, which is equivalent: A = Tue; It should also be noted that enumeration elements are neither character constants nor string constants. Do not add single or double quotation marks when using them. [Example 11.11] Main (){ Enum body {A, B, c, d} month [31], J; Int I; J =; For (I = 1; I <= 30; I ++ ){ Month [I] = J; J ++; If (j> d) J =; } 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 "); }