"C Language" 22-enumeration

Source: Internet
Author: User

First,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,......};

Enum

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 an enumeration variable while defining an enumeration type
enum Season {spring, summer, autumn, winter} s;

3. Omitting enumeration names, directly defining enumeration variables
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 compiler will treat 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   Season {spring, summer, autumn, winter} s;s  = spring; //  s  = 3 ; //  

2. Traversing an enumeration element
enumSeason {Spring, summer, autumn, winter} s;//traversing an enumeration element for(s = spring; s <= Winter; s++) {printf ("enumeration element:%d \ n", s);}

Output Result:

Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.

"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.