"C Language" 22-enumeration

Source: Internet
Author: User

directory of this document
    • First, the concept of enumerations
    • II. definitions of enumeration types
    • Third, the definition of the enumeration variable
    • Iv. Considerations for enumeration use
    • V. Basic operation of enumeration variables

Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore

The previous section describes the struct type, which describes another type of data type in the C language---enumeration types. Enumeration types are also commonly used in iOS, similar to enumerations in Java.

Back to TopFirst, the concept of enumerations

Enumerations are a basic data type in the C language, not a constructed type, which can be used to declare a set of constants. When a variable has several fixed possible values, the variable can be defined as an enumeration type. For example, you can use a variable of an enumeration type to represent the season, because there are only 4 possible values for the season: Spring, summer, autumn, and winter.

Back to TopII. definitions of enumeration types

The general form is:Enum enumeration Name {Enumeration element 1, Enumeration element 2,......};

Back to Topthird, the definition of the enumeration variable

Just before you define the enumeration type, you can then define the variable with the defined enumeration type.

As with structs, there are 3 ways to define enumeration variables

1. Define the enumeration type first, and then define the enumeration variables
Enum Season {Spring, summer, autumn, winter};  Enum Season s; 

2. Defining enumeration variables while defining enumeration types
Enum Season {Spring, summer, autumn, winter} s;

3. Omit the enumeration name and define the enumeration variables directly
enum {spring, summer, autumn, winter} s;

All three of the above are defined as enumeration variables s

Back to TopIv. Considerations for enumeration use

The 1> C language compiler handles enumeration elements (spring, summer, and so on) as integer constants, called enumeration constants.

The value of the 2> enumeration element depends on the order in which the enumeration elements are arranged when defined. By default, the first enumeration element has a value of 0, the second is 1, and sequentially adds 1.

Enum Season {Spring, summer, autumn, winter};

That is, spring has a value of 0,summer value of 1,autumn and a value of 2,winter is 3

3> can also change the value of an enumeration element when defining an enumeration type

Enum season {spring, summer=3, Autumn, winter};

An enumeration element with no value specified, with a value of 1 for the previous element. It is also said that the value of spring is 0,summer value of 3,autumn is 4,winter value is 5

Back to Topv. Basic operation of enumeration variables1. Assigning Values

Enumeration constants or integer values can be assigned to enumeration variables

Enum// equivalent to S = 0;  equivalent to S = Winter;   

2. Traversing an enumeration element
Enum Season {Spring, summer, autumn, winter} s;  iterate over the enumeration element for (s = spring; s <= Winter; s++) {printf (" enum element:%d \ n", s);}   

Output Result:

"C Language" 22-enumeration

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.