C Language Basics Tutorial (iv) Pointers, structures, unions, and enumerations (17)

Source: Internet
Author: User
Tags constant integer

Enum (enum)

Enumerations are a collection of named Integer constants, which are common in everyday life.
For example, the sunday,monday,tuesday,wednesday,thursday,friday,saturday of the week is an enumeration.
The descriptions of enumerations are similar to structs and unions in the form of:
Enum Enumeration Name {
identifier [= integer constant],
identifier [= integer constant],
...
identifier [= integer constant],
Enumerate variables;
If the enumeration is not initialized, that is, when the "= Integer Constant" is omitted, the first identifier begins, and the identifier 0,1,2 is assigned sequentially .... However, when a member of an enumeration is assigned a value, subsequent members determine its value by the rule of 1 in turn.
For example, after the following enumeration descriptions, the x1,x2,x3,x4 values are 0,1,2,3.
Enum string{x1, x2, X3, x4}x;
When the definition changes into:
Enum string
{
X1,
X2=0,
X3=50,
X4,
}x;
Then X1=0, x2=0, x3=50, x4=51
Attention:
1. Each member (identifier) Terminator in the enumeration is ",", not ";", and the last member can omit ",".
2. You can assign a negative number when initializing, and then the identifier will add 1 in turn.
3. An enumeration variable can only take an identifier constant in the enumeration description structure.
For example:
Enum string
{
X1=5,
X2,
X3,
X4,
};
Enum Strig x=x3;
At this point, the enumeration variable x is actually 7.

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.