Enumeration is convenient for user-defined fixed data groups. An enumeration class is a series of constant integer values, which means that the enumeration type cannot be modified.
Here we will discuss the usage and limitations of enum types in C.
Enumerations are defined by enumerating key values, similar to struct definitions
Syntax (Syntax):
enum tagname{const1=1, CONST2,...} enumvariable;
tagname, enumeration type name, this item can be omitted.
{Const1=1,const2,...} A set of strings that replace constant values, each corresponding to an integer data, the shaping data starting at 0, the second being 1, and so on. The ordinal of a string is the integer value of the string, unless the user defines the class as const1=1. If the user specifies the integer value of the string itself, then the corresponding value of the string that follows it is followed by the successive values. That's not clear, for example, Const2=6, the value of the element behind this element is the corresponding 7,8,9, and so on.
enumvariable A list of variables for the enumeration type. The variables of the enumeration type are integral types, except for enumeration constants, which can be assigned to any integer data. This definition is also optional, like a struct, or it can be defined in a later program:
enum tagname enumvariable;
The following is an example of enumeration type usage:
#include <stdio.h>//defining the enumenumWeek{monday= -1, tuesday,wednesday,thursday=6, friday,saturday,sunday}; intMainvoid){ //declaring an enum in the program enumWeek day=FRIDAY; Switch(day) { Casemonday:printf ("Monday Sucks"); Break; Casesaturday:printf ("Weekends is fun"); Break; Casesunday:printf ("Weekends is fun"); Break; default: printf ("It depends on the mood:D%d", day); Break; } return 0;}
Uncover the veil of the enumeration class (unlocking the Enumeration/enum Mystery)