YouTip LogoYouTip

C Enum

# C enum(Enumeration) An enumeration is a fundamental data type in the C language used to define a set of constants with discrete values. It makes data more concise and easier to read. Enumeration types are typically used to assign names to a group of related constants in a program, improving the program's readability and maintainability. To define an enumeration type, you use the `enum` keyword, followed by the name of the enumeration type, and a set of enumeration constants enclosed in curly braces `{}`. Each enumeration constant can be represented by an identifier, and you can also assign an integer value to them. If no value is specified, they default to incrementing from 0. The syntax for defining an enumeration is: enum enumeration_name { enumeration_element1, enumeration_element2, ... }; Next, let's look at an example. For instance, there are 7 days in a week. Without using an enumeration, we would need to use `#define` to define an alias for each integer: #define MON 1#define TUE 2#define WED 3#define THU 4#define FRI 5#define SAT 6#define SUN 7 This looks like a lot of code. Now let's see how to use an enumeration: enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN }; This looks much more concise. **Note:** The default value of the first enumeration member is 0 for the integer type, and the values of subsequent enumeration members increment by 1 from the previous member. In this example, we defined the value of the first enumeration member as 1, so the second is 2, and so on. > You can change the values of enumeration elements when defining the enumeration type: > > enum season {spring, summer=3, autumn, winter}; > Enumeration elements without specified values have a value equal to the previous element plus 1. That is, the value of `spring` is 0, `summer` is 3, `autumn` is 4, and `winter` is 5. ### Defining Enumeration Variables Previously, we only declared the enumeration type. Now let's see how to define enumeration variables. We can define enumeration variables in the following three ways: **1. Define the enumeration type first, then define the enumeration variable** enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };enum DAY day; **2. Define the enumeration variable while defining the enumeration type** enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN } day; **3. Omit the enumeration name and define the enumeration variable directly** enum{ MON=1, TUE, WED, THU, FRI, SAT, SUN } day; ## Example #includeenum DAY{MON=1, TUE, WED, THU, FRI, SAT, SUN}; int main(){enum DAY day; day = WED; printf("%d",day); return 0; } The output of the above example is: 3 In C, enumeration types are treated as `int` or `unsigned int` types, so according to the C language specification, it is not possible to traverse enumeration types. However, in some special cases, if the enumeration type is continuous, conditional traversal can be achieved. The following example uses a `for` loop to traverse the enumeration elements: ## Example #includeenum DAY{MON=1, TUE, WED, THU, FRI, SAT, SUN}day; int main(){// Traverse enumeration elements for(day = MON; day<= SUN; day++){printf("Enumeration element: %d n", day); }} The output of the above example is: Enumeration element: 1 Enumeration element: 2 Enumeration element: 3 Enumeration element: 4 Enumeration element: 5 Enumeration element: 6 Enumeration element: 7 The following enumeration type is not continuous and cannot be traversed. enum{ ENUM_0, ENUM_10 = 10, ENUM_11 }; Using enumerations in a `switch` statement: ## Example #include#includeint main(){enum color{red=1, green, blue}; enum color favorite_color; /* User inputs a number to select a color */printf("Enter your favorite color: (1. red, 2. green, 3. blue): "); scanf("%u", &favorite_color); /* Output the result */switch(favorite_color){case red: printf("Your favorite color is red"); break; case green: printf("Your favorite color is green"); break; case blue: printf("Your favorite color is blue"); break; default: printf("You did not select your favorite color"); }return 0; } The output of the above example is: Enter your favorite color: (1. red, 2. green, 3. blue): 1Your favorite color is red ### Converting an Integer to an Enumeration The following example converts an integer to an enumeration: ## Example #include#includeint main(){enum day{saturday, sunday, monday, tuesday, wednesday, thursday, friday}workday; int a = 1; enum day weekend; weekend = (enum day)a; // Type conversion//weekend = a; // Error printf("weekend:%d",weekend); return 0; } The output of the above example is: weekend:1
← Prop Cssstyle CsstextPython3 Func Filter β†’