Use of Enum in C

Source: Internet
Author: User

Suppose you need several possible values for a variable, then it can be defined as an enumeration type. The reason for enumeration is to enumerate the possible values of variables or objects.

For example, to make everyone more clear, for example, there is a pen in a pencil box, but you don't know what it is before you open it, it may be a pencil or a pen. There are two possibilities, so you can define an enumeration type to represent it!

Enum box {penpencil, pen}; // here you define a variable of Enumeration type called box, the enumerated variable contains two elements, the pencil and pen, respectively.

Let's talk about it. If you want to define two variables with the same feature Enumeration type, you can use the following two methods to define them!

Enum box {penpencil, pen };

Enum box box2; // or abbreviated to box box2;

Another type is defined at the same time during the declaration.

Enum {penpencil, pen} box, box2; // define the statement at the same time!

The enumeration Element System in enumeration variables is processed according to constants.Enumerated constantThey cannot assign values to ordinary arithmetic tasks. (penpencil = 1;) This write is wrong, but you can assign values when declaring them!

Enum box {penpencil = 1, pen = 2 };

However, it should be noted that, if you do not assign values to elements, the elements will be automatically assigned by the system starting from 0, when it comes to assigning values by yourself, if you only define the first one, the system will add 1 to the value of the previous element to the next element, for example

Enum box {penpencil = 3, pen}; // here the pen is 4. The system will take the initiative to perform the pen = 4 Definition assignment operation!

As mentioned above, the following is a complete example. You can learn more completely through the following code!

# Include <iostream>
Using namespace STD;

Void main (void)
{
Enum egg {a, B, c };
Enum egg test; // here you can abbreviated it as egg test;

Test = C; // assign the element operation to the enumerated variable test. Here, the element assignment operation is not called the value assignment operation to make it clear that the enumerated variable cannot be directly assigned a value, for example, (test = 1;) is not accepted by the compiler. The correct method is to force type conversion first, for example (test = (Enum egg) 0 ;)!

If (test = C)
{
Cout <"enumeration variable inference: The enumerated element of test enumeration is C" <Endl;
}

If (test = 2)
{
Cout <"enumeration variable inference: the value of the test enumeration element is 2" <Endl;
}

Cout <A <"|" <B <"|" <test <Endl;

Test = (Enum egg) 0; // force type conversion
Cout <"the test value of the enumerated variable is changed to:" <test <Endl;
Cin. Get ();
}

The final problem here is that the enumerated elements (or enumeration constants) in the enumerated variables are automatically upgraded to the arithmetic type in special circumstances!

# Include <iostream>
Using namespace STD;

Void main (void)
{
Enum test {a, B };
Int c = 1 + B; // automatically upgrades itself to the arithmetic type
Cout <C <Endl;
Cin. Get ();
}

 

 

 

Enumeration type
In practice, the value of some variables is limited to a limited range. For example, there are only seven days in a week, twelve months in a year, and six courses per week in a class. Assume that it is obviously inappropriate to describe these values as integer, dense, or other types. Therefore, the C language provides a type called "enumeration. All possible values are listed in the definition of the "enumeration" type. The value of the variable described as this "enumeration" type cannot exceed the defined range. It should be noted that the enumeration type is a basic data type, rather than a construction type, because it cannot be broken down into any basic type.
11.10.1 definitions of enumeration types and description of enumeration Variables
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. The enumerated values share seven, 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 explained at the same time or directly.
The variables A, B, and C are described as the preceding weekday, which can be used in either of the following ways:
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;
11.10.2 assignment and use of enumerated type variables
The following rules apply to enumeration types:
1. The enumerated value is a constant, not a variable. You cannot assign values to a value in a program.
For example, assign the following value to the element listing 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, starting from 0 to 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 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 the enumeration element is neither a character constant nor a String constant. Do not add single or double quotes when using it.
[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 ");
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.