"Learning note", "C language" 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.
1. The general form is: Enum enumeration Name {Enumeration element 1, Enumeration element 2,......};

For example:
Enum Season {
Spring
Summer
Autumn
Winter
};

Second, the use of attention

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

1#include <stdio.h>2 3 intMain ()4 {5     enumSex {man, Woman, unkown};6     7     //0 Male 1 female-1 unspecified8     //int sex = 3;9     //enum Sex s = unkown;Ten      One     //1. Defining Enum Types A     enumSeason -     { -Spring =1, the Summer, - Autumn, - Winter -     }; +      -     //2. Defining enumeration Variables +     enumSeason s =100000; A      at      -printf"%d\n", s); -      -      -     return 0; -}

"Learning note", "C language" enumeration

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.