-
enum is used to declareEnumeration typeData.
It can store many elements like an array, but unlike an array, it cannot store other types of letters, such as letters and special symbols, except for numbers.
Chinese characters can be stored in any combination of enum.
Enum's statement: enum a;
Enum declarations, unlike arrays, can only be declared outside of the main() method and can be called in main().
By default, enum's enumeration values start at 0, but it is also possible to force the sequence of elements to change. enum a ;This makes it possible.
enum can be accessed by enumeration name plus dots by enumerating the elements of the enumeration. Example:
int i = (int);Enumerated values, although of type int, must also be cast for display.
Similarly, enum can also access individual elements within an enumeration by enumerating numeric values. Method:" (a)1 " );This gives access to the various elements within the enum.
-
The usage of the enum type enum is shown below:
Enumeration is the abbreviation of enumerate, that is, the meaning of enumeration, arrangement. An enumeration type is a special type in the C language that allows our program to use a range of variable values of fixed length and fixed values. Define the enumeration type:
enum enumeration type ;
1. Define the enumeration type.
2. If you specify the value of the element when defining the enumeration type, you can also change the value of the enumeration element.
For example: enum weekdayday;
At this time, sun is 7, mon is 1, and the elements are added by 1 in order, so the sat is 6.
3. The enumeration value can be used to make judgments.
For example: if (day==month).
if (day>month)
The rules for comparing enumerated values are that they are numbered in the order in which they are described, and if no one is artificially specified at the time of description, the value of the first enumerated element is considered to be 0.
4. An integer cannot be directly assigned to an enumerated variable, and a type conversion must be forced to assign a value.
-
1.The general form of an enumeration type definition is:
enum enumeration name;
All available values should be listed in the enumerated values table. These values are also known as enumeration elements.
For example, the enumeration is called weekday, and there are 7 enumeration values, i.e. 7 days of the week. The value of a variable of weekday can only be one of the seven days.
2.Description of the enumerated variables.
Like structs and unions, enumerated variables can be described in different ways, i.e. defined first and then specified, defined or directly specified.
-
Enumerations are constants, e.g., enum day;
main()
a,b,c;
a=sun;
b=mon;
c=tue;
printf("%d,%d,%d",a,b,c);}
-
You're talking about the C enumeration type. An enumeration is a set of named integer constants.
The description of enumeration is similar to structure and union, and its form is: enum enumeration name enumeration variable;
If the enum is not initialized, it is omitted"= integer constant", the identifiers 0, 1, 2, ., are assigned starting with the first identifierHowever, when a member in the enumeration is assigned, the subsequent members determine their values according to the rule of adding 1 in order.
Note:1Negative numbers can be assigned during initialization, and subsequent identifiers are still added by 1 in turn.
2.An enumerated variable can only take an identifier constant in the enumeration description structure.
3.Enumerated values are constants, not variables. You can't assign a value to it in a program with an assignment statement. Self-incrementing and self-decrement operations are not possible.
4.Only enumerated values can be assigned to enumerated variables, and the numerical values of elements cannot be directly assigned to enumerated variables.
5.Enumerated elements are not character constants or string constants, and should not be used with single or double quotation marks.
-
The main purpose of enumeration, I think, is to make ** more readable. If there is no enumeration, the function it is intended to achieve can be achieved in other ways.
enum enumeration name;
The enumeration value is a symbol in itself, but it has an int property. You can read the book, so I won't say more. Enumerated values are names that tell you what they really mean.
For example, an enumeration type of a playing card suit.
enum color
You can also default to 0 for heart, 1 for spade, 2 for diamond, and 3 for club. When judging the suit, if(color==0)...
else if(color==1)..
else if(color==2)..
else...
You can also use the define definition.
#define heart 0
#define spade 1
#define diamond 2
#define club 3
But so many defines are obviously unnecessary, and enum won't let you write so many defines.
To access enumerated variables, use enumeration names. Enumeration values.
This way, when you appear elsewhere in the program, you can clearly see that the card is of the suit of hearts.
-
I'll add one more thing: another important role of enums is as compile-time constants.
-
#include
enummain()
If it is helpful to you, please remember to accept it as a satisfactory answer, thank you! Have a great day!
The output of this program is: 4
You can standardize the program to make it easier to understand. >>>More
The p of both is a pointer.
p=&t, change the content that p points to to the address of t, and p=&t is the address that changes the pointer to t. >>>More
Neither enumeration nor pointer types are basic data types. >>>More
I can't remove it. like to do sth.
Or like doing sthIt is a fixed phrase, like and play are both verbs, and they are predicates in the sentence. >>>More
Is there a complete skill with or without it? The blasted teleportation orb is a must-add for firing a magic cannon. The little jump of the frantic ** proficient in the upper pick is also a must. The blasting ball must be full, the frenzied small jump and ** must be full, and the rest is completely up to the direction of the plus point.