[C Language] 22-Enumeration

Source: Internet
Author: User

Note: This C language topic is a prelude to iOS development. To enable programmers with object-oriented language development experience to quickly get started with the C language. If you have no programming experience or are not interested in C and iOS development, ignore

The previous section introduced the struct type, which introduces another data type in C language-Enumeration type. The enumeration type is also very common in iOS, and its usage is similar to that in Java.

I. Enumeration Concept

Enumeration is a basic data type in C, not a constructor type. It can be used to declare a group of constants. When a variable has several fixed possible values, you can define this variable as an enumeration type. For example, you can use an enumerated variable to represent the season, because there are only four possible values in the season: spring, summer, autumn, and winter.

 

Ii. Enumeration type definition

The general format is: enum enumeration name {enumeration element 1, enumeration element 2 ,......};

enum Season {spring, summer, autumn, winter}; 

 

Iii. Definitions of enumerated Variables

Previously, only the enumeration type is defined. Next, you can use the defined Enumeration type to define variables.

Like struct, there are three ways to define enumeration Variables

1. First define the enumeration type and then the enumeration variable
enum Season {spring, summer, autumn, winter};enum Season s;

 

2. Define both enumeration types and enumeration Variables
enum Season {spring, summer, autumn, winter} s;

 

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

The preceding three methods define the enumerated variable s.

 

Iv. Enumeration

1> the C language compiler processes enumeration elements (such as spring and summer) as Integer constants, which are called enumeration constants.

2> the values of enumeration elements depend on the order in which enumeration elements are arranged during definition. By default, the value of the first enumeration element is 0, the second is 1, and 1 is added sequentially.

enum Season {spring, summer, autumn, winter};

That is to say, the value of spring is 0, the value of summer is 1, the value of autumn is 2, and the value of winter is 3.

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

enum season {spring, summer=3, autumn, winter};

An enumeration element without a specified value. Its value is 1 added to the previous element. In other words, the spring value is 0, the summer value is 3, the autumn value is 4, and the winter value is 5.

 

5. Basic operations on enumerated variables 1. Assignment

You can assign an enumerated constant or an integer value to the enumerated variable.

Enum Season {spring, summer, autumn, winter} s; s = spring; // equivalent to s = 0; s = 3; // equivalent to s = winter;

 

2. Traverse enumeration Elements
Enum Season {spring, summer, autumn, winter} s; // traverses the enumeration element for (s = spring; s <= winter; s ++) {printf ("enumeration element: % d \ n ", s );}

Output result:

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.