30 Enumeration

1, the enumeration data type is a construct in C language, which allows data to be more concise and easier to read, for only a limited number of specific data, you can use enumeration

2, corresponding to enumerate enumeration English shorthand enum

3, is a set of enumeration constants, specific data contains a set of limited

4, enumeration syntax definition format  

  enum enum {name

    Enumeration element 1, element 2 enumeration, enumeration element 3, .......

  } ;

  

 

 

   Print Results:

  day = 3

5, the enumeration traversal

  C, as enumerated type is an unsigned int int or handled, enumerated type must traverse conditions can be achieved when a continuous

. 1 #include <stdio.h>
 2  enum DAY {
 . 3      the MON = . 1 , the TUE, WEDs, the THU, the FRI, the SAT, the SUN   // If no assignment, the assignment will be in the order 
. 4 } Day;   // represents a definition of enumeration type enum DAY, while a variable is defined day (DAY type enum) 
. 5  
. 6  void main () {
 . 7  
. 8      for (= the MON Day; Day <= the SUN; Day ++) {   // requires continuous assignments enumeration element 
9          the printf ( " enumeration element: D% \ n- " , Day);
 10      }
 . 11 }

 

 

 6, switch is used to enumerate

1 #include <stdio.h>
 2  
. 3  void main () {
 . 4      enum SEASONS {
 . 5          SPRING = 1 , SUMMER, AUTUMN, WINTER
 . 6      };
 . 7      enum SEASONS Season;
 . 8      the printf ( " Please enter your favorite season (1 : Spring, 2: Summer,. 3, Autumn,. 4, Winter): " );
 . 9      Scanf ( " % D " , & season);
 10      Switch (season) {
 . 11      Case SPRING:
 12 is          the printf ( " your favorite season spring " );
13          BREAK ;
 14      Case SUMMER:
 15          printf ( " Your favorite season is summer " );
 16          BREAK ;
 17      Case AUTUMN:
 18          printf ( " Your favorite season is autumn " );
 19          BREAK ;
 20      Case WINTER:
 21          printf ( " your favorite season is winter " );
 22          BREAK ;
 23      default :
 24-          printf ( " you do not choose your favorite season " );
25     }
26 }

 

 

 7, the default value is the first member of the enumerated integer 0, values ​​of subsequent enumeration member on a front member plus 1,

8, in the definition of an enumerated type can change the value of the enumerated elements:

  enum DAY {

    Mon, Tue, Wed, SEE = 9, Fri, SAT, Sun

  } day ; 

  Value THU, SAT, SUN is 10.11,12

9, in the form of an enumeration defined, enumerated types defined first, then the definition of enumeration

  enum DAY {

    Mon, Tue, Wed, Thu, Fri, SAT, Sun

  } ;

     enum DAY day;

10, the enumeration defined in the form 2, the definition of an enumerated type defined enumeration colleague

     enum DAY {

    Mon, Tue, Wed, Thu, Fri, SAT, Sun

   }  day ;

11, the enumeration defined in the form 3, the enumeration name is omitted, directly define the enumeration

  enum  {

    Mon, Tue, Wed, Thu, Fri, SAT, Sun

  } day  ;

  Such use enumeration type can only be used once

12, can be converted to the corresponding integer value enum

  Can not directly be an integer, assigned enumeration, but may be integers, enumerated types turn into, and then assign enumeration

  enum SEASONS {  SPRING=1 , SUMMER , AUTUMN , WINTER } ;

  enum SEASONS  season ;

  int n=4;

  season = (enum SEASONS) n ;

  

 

Guess you like

Origin www.cnblogs.com/shanlu0000/p/12348042.html