typedef enum and enum verbose usage

Source: Internet
Author: User
Enumeration (enum) links a string of names with a string of reshaping values in a simple way. Can replace the #define Declaration

Enumerate in general form:

Enum Optional label {content ...} Optional definition variable definition;

Where "content ..." is a list of identifiers and may have some integer values assigned to them.

Enum asn{mon=7, TUE, wed=10, THU, FRI};

1.ASN is an identifier that can be viewed as the name of the set, an optional, or optional item.

2. By default, integer values start at 0. If an identifier in the list is assigned, then the value of that identifier immediately after is greater than the assigned value, and then the analogy.

3. End With;

An enum can access an enumerated value through an enumerated element by using an enum name dot. Cases:
int i = (int) ASN. MON enumerated values are of type int, but must also be cast for display.

You can assign an enumeration value only to a variable, not directly to an enumeration variable, unless the cast

Enum Day
{
Mon=1, TUE, WED, THU, FRI, SAT, SUN
};

Enum Day Good_day, Bad_day; Variable good_day and Bad_day types are enumerated enum day

Enum//Is different from the first definition, the label day here is omitted, which is allowed.
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
} workday; The type of variable workday is enum type enum day


Enum BOOLEAN {false, true} End_flag, Match_flag; Defines an enumeration type and declares two enumerated variables

typedef enum Usage

typedef enum WORKDAY
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
};

Workday today, tomorrow; The types of variables today and tomorrow are enumerated workday, that is, enum workday

Note: Enumeration types with the same name cannot be defined in the same program, and named constants with the same name cannot exist in different enumeration types.

Advantages: #define定义的名字在编译时被丢弃, while enumerated names are usually visible in the debugger and can be used when debugging code.

Program Examples:


Ride

Output 0,1,2


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.