The use of enum in C

Source: Internet
Author: User


Assuming that you need several possible values for a variable, you can be defined as an enumeration type. The enumeration means that it is possible to say that a variable or an object that is called a possible value is one by one cases.

Give a sample to illustrate one, in order to make it clearer, for example, a pencil case has a pen, but you do not know what it is before opening the pen, may be a pencil or pen, there are two possibilities, then you can define an enumeration type to represent it!

enum Box{pencil,pen}; // Here you define a variable of enum type called Box, which contains two elements, also known as enumeration elements here are pencil and pen, respectively, for pencils and pens.

Here's to say, suppose you want to define two variables with the same enumeration type of attributes then you can define them in two ways, such as the following:

enum Box{pencil,pen};

enum box Box2; // or shorthand into box box2;

Another is defined at the same time in the declaration.

enum {pencil,pen}box,box2; // be defined at the same time as the declaration!

Enumeration elements in enumeration variables the system is handled according to constants , so called enumeration constants , they are not able to carry out ordinary arithmetic assignment, (pencil=1;) This kind of write is wrong, but you can be in the declaration of the value of the operation!

enum box{pencil=1,pen=2};

However, it is important to note that if you do not perform the element assignment, then the element will be actively increment the system itself from 0 to do the assignment, when it comes to self-assignment, assuming you only define the first one then the system will be the next element of the value of the previous element plus 1 operations, such as

Enum Box{pencil=3,pen}; // here Pen is the 4 system will be the active pen=4 of the definition assignment operation!

Before saying so much, the following gives a complete sample of people can learn through the following code to more complete learning!

#include <iostream>
usingnamespaceStd

voidMain(void)
{
enumEgg {a,b,c};
enumEgg test;//Here you can abbreviate it into an egg test;

Test=C;//to the enumeration variable testTo give an element operation, here is called the assignment operation is not called assignment operation is to let everyone clear enumeration variables can not be directly assigned to the arithmetic value, such as (test=1;) This operation is not accepted by the compiler, the correct way is to first make a forced type conversion such as (test=(enum egg) 0;)!

if(TEST==C)
{
cout<< "Enumeration variable inference: Test enumeration Corresponding enumeration element is C" << Endl;
}

if(test==2)
{
cout<< enum Variable inference: The value of the test enumeration element is 2 "<< Endl;
}

cout<< a << "|" << b << "|" << test <<endl;

Test=(enumEgg) 0;//Forcing type conversions
cout<< "enumeration variable test value changed to:" << test <<endl;
Cin. get ();
}

The last thing you see here is that the enumeration element in the enumeration variable (or enumeration constant) is promoted to arithmetic type by itself in special cases!

#include <iostream>
using namespace std;

void Main (void)
{
enum Test {a, b};
int c=1+b; // self-promotion to arithmetic type
cout << c <<endl;
cin. get ();
}

Enum type
In practical problems, the values of some variables are confined to a limited range. For example, there are only seven days in one weeks, only 12 months a year, one class has six courses per week and so on. It is not appropriate to assume that these quantities are described as integers, and that character or other types are clearly inappropriate. For this reason, the C language provides a type called Enum. All possible values are enumerated in the definition of the "enum" type, and the value of the variable that is described as the "enum" type cannot exceed the defined range. It should be stated that an enumeration type is a basic data type, not a constructed type, since it cannot be decomposed to any basic type.
11.10. Description of the definition of the 1 enumeration type and the enumeration variable
1. Enumeration defines the general form of the enumeration type definition:
enumEnumeration Name {enumeration value Table};
In the enumeration values table, all available values are listed in ROM. These values are also known as enumeration elements.
Like what:

The enumeration is named weekday, and the enumeration values collectively have 7, which is seven days in a week. Any value that is described as a variable of type weekday can only be one day in seven days.
2. Description of the enumeration variable
As with structs and unions, enumeration variables can also be described in different ways, namely, defining the post-description, defining the description at the same time, or directly stating it.
The variable a,b,c is described above as weekday, which can be used in any of the following ways:
Enum weekday{Sun,mou,tue,wed,thu,fri,sat};
Enum weekday a,b,c;
or for:
Enum weekday{Sun,mou,tue,wed,thu,fri,sat}a,b,c;
or for:
enum {Sun,mou,tue,wed,thu,fri,sat}a,b,c;
11.10. Assignment and use of 2 enumerated type variables
Enumeration types are used in the following rules:
1.enumeration values are constants, not variables。 You cannot assign a value to an assignment statement in a program.
For example, the elements of the enumeration weekday are assigned the following values:
sun=5;
mon=2;
Sun=mon;
are all wrong.
2. The enumeration element itself is defined by the system as a numeric value representing the ordinal, from the beginning of the 0 sequence defined as 0,1,2 .... As in weekday, the Sun value is 0,mon value of 1,...,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);
}

Description
can only putenumeration values are assigned to enumeration variables, you cannot assign the value of an element directly to an enumeration variable. Such as:
A=sum;
B=mon;
is correct. and
a=0;
B=1;
Is wrong. If you must assign a value to an enumeration variable, you must cast with a coercion type.
Such as:
A= (enum weekday) 2;
The implication is that the enumeration element with sequence number 2 is given an enumeration variable A, which is equivalent to:
A=tue;
It should also be stated thatenumeration elements are not character constants or string constants, and do not use single or double quotes.
"Example 11.11"
Main () {
Enum body
{a,b,c,d} month[31],j;
int i;
J=a;
for (i=1;i<=30;i++) {
Month[i]=j;
j + +;
if (j>d) j=a;
}
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");
}

The use of enum in C

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.