Enumeration of basic IOS data types and IOS Data Types

Source: Internet
Author: User
Tags integer numbers

Enumeration of basic IOS data types and IOS Data Types

Enumeration is a basic data type in C language. By enumeration, you can declare a group of constants to represent different meanings. It is actually a set of Integer constants.

Enumeration is a very common type and is also common in real life. For example, if there are four seasons, different colors need to be displayed in different seasons. In the code, we can use one to four numbers to represent different seasons. However, in this case, the program is poorly readable, it is difficult to tell which season different numbers represent. At this time, you can use an enumerative variable of the season to represent spring, summer, autumn, and winter, in this way, we can know the corresponding season when we see the first time of the enumerated value, but in fact the four enumerated values correspond to four integer numbers.

Enumeration type definition

Enum enumeration name {enumeration element 1, enumeration element 2, enumeration element 3... };

Code

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

Enumeration variable definition

After defining the enumerated type according to the format, you can declare the enumerated variable using the defined enumerated type. The following uses four seasons as an example to illustrate how to define enumerated variables.

1. First define the enumeration type and then the enumeration variable

Code

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

2. Define both enumeration types and enumeration Variables

Code

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

3. Omit the enumeration name and define the enumeration variable directly.

Code

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

4. Rename the enumeration type and define the enumerated variables.

Code

typedef enum _season {spring, summer, autumn, winter} Season;     Season s;

Values corresponding to enumeration Elements

1. The value of an enumeration element depends on the order in which the enumeration type is defined. By default, the value of the first enumeration element is 0, the second is 1, and the value of the next enumeration element is 1.

Code

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

In this example, the spring value is 0, the summer value is 1, The autuum value is 2, and the winter value is 3.

2. You can change the value of an enumeration element when defining the enumeration type. Specify a value for an enumeration element, and Add 1 to the enumerated value after the enumeration element.

  Code

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

In this example, the spring value is 0, the summer value is 3, The autuum value is 4, and the winter value is 5.

Values corresponding to enumeration Elements

After defining the enumeration type, you can use the instance variables of the enumeration type in the class or use the enumeration type parameters in the method. You can use enumerated constants or integer values to assign values to enumerated variables.

Code

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

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

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.