Key Points of C language enum Enumeration

Source: Internet
Author: User

Three enumeration methods

(1)

# Include
 
  
Enum ENUM_TEST {ENUM_EST1 = 1, ENUM_EST2, ENUM_EST3, hour, ENUM_EST5, hour, ENUM_EST10,}; int main () {enum ENUM_TEST enum_test; enum_test = ENUM_EST10 + 1; printf ("enum_test = % d \ n", enum_test); return 0 ;}result: enum_test = 11
 

(2)

# Include
 
  
Enum {ENUM_EST1 = 1, ENUM_EST2, numbers, ENUM_EST4, numbers, ENUM_EST9, ENUM_EST10,} ENUM_TEST; int main () {ENUM_TEST = ENUM_EST10 + 1; printf ("enum_test = % d \ n", ENUM_TEST); return 0;} result: enum_test = 11
 

(3)

# Include
 
  
Typedef enum {ENUM_EST1 = 1, ENUM_EST2, ENUM_EST3, numeric, ENUM_EST5, numeric, ENUM_EST10,} ENUM_TEST; int main () {ENUM_TEST enum_test; enum_test = ENUM_EST10 + 1; printf ("enum_test = % d \ n", enum_test); return 0 ;}result: enum_test = 11
 

Note 1:

Assign a value to the enumerated variable. If the value exceeds the enumerated value range, the result is uncertain, as shown in the preceding three examples.

NOTE 2:

The last enumeration element can have either "," or "no"

NOTE 3:

The first method is to declare the enum_test variable with enum ENUM_TEST instead of ENUM_TEST. Both methods can be used in C ++.

Both the first and third types are declared;

The second and third usages, although only one typedef keyword is different, have different meanings. The second usage declares an enumeration variable, the third method is to declare an enumeration type.

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.