Usage of enum and typedef enum

In a program, it may be necessary to define an alias for some integers, we can make use of the preprocessing directive #define to do this, your code may be:

#define MON 1
#define TUE 2
#define WED 3
#define THU 4
#define FRI 5
#define SAT 6
#define SUN 7
 
Here we define a new data type that we hope will do the same job. This new data type is called an enumeration.
1. Define a new data type - enumeration type
 The following code defines this new data type - enumeration type
enum DAY
{
      MON=1, TUE, WED, THU, FRI, SAT, SUN
};
(1) An enumeration type is a collection, and the elements in the collection (enumeration members) are some named integer constants, and the elements are separated by commas.
(2) DAY is an identifier, which can be regarded as the name of the set, and is an optional item, that is, an optional item.
(3) The default value of the first enumeration member is an integer of 0, and the value of subsequent enumeration members is increased by 1 to the previous member.
(4) You can manually set the value of the enumeration members to customize the integers in a certain range.
(5) The enumeration type is an alternative to the preprocessing directive #define.
(6) The type definition ends with a semicolon;.
 
2. Declare variables with enumeration types
Once the new data type is defined, it is ready to use. We have seen the most basic data types, such as: integer int, single-precision floating-point type float, double-precision floating-point type double, character type char, short integer type short and so on. Declare variables with these basic data types usually like this:
char a; //variable a is of type char
char letter;
int x,
           y,
           z; //variables x, y and z are of type int
int number;
double m, n;
double result; //The type of variable result is double-precision floating-point double
 
Since enumeration is also a data type, it can also declare variables like basic data types.
Method 1: The definition of the enumeration type is separated from the declaration of the variable
enum DAY
{
      MON=1, TUE, WED, THU, FRI, SAT, SUN
};
 
enum DAY yesterday;
enum DAY today;
enum DAY tomorrow; //variable tomorrow The type is enumeration enum DAY
enum DAY good_day, bad_day; //The types of variables good_day and bad_day are enumeration enum DAY
 
Method 2: Type definition and variable declaration are carried out at the same time:
enum //Unlike 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 enumeration enum DAY
 
enum week { Mon=1, Tue, Wed, Thu, Fri Sat, Sun} days ; //The type of the variable days is enumeration type enum week
 
enum BOOLEAN { false, true } end_flag, match_flag; //Define the enumeration type and declare two enumeration type variables
 
Method 3: Use the typedef keyword to convert the enumeration type Define it as an alias and use the alias for variable declaration:
typedef enum workday
{
    saturday,
    sunday = 0,
    monday,
    tuesday,
    wednesday,
    thursday,
    friday
} workday; //workday here is the alias of enum workday
 
workday today, tomorrow; // the types of variables today and tomorrow are enum workday, that is, enum workday
 
can also be:
typedef enum
{
    saturday,
    sunday = 0 ,
    monday,
    tuesday,
    wednesday,
    thursday,
    friday
} workday; //workday here is the alias of enum workday


workday today, tomorrow; //the types of variables today and tomorrow are enum workday, that is, enum workday
 
is also Can be used this way:
typedef enum workday
{
    saturday,
    sunday = 0,
    monday,
    tuesday,
    wednesday,
    thursday,
    friday
};


workday today, tomorrow; //The types of the variables today and tomorrow are enumeration type workday, that is, enum workday
 
Note: The enumeration type with the same name cannot be defined in the same program, and the named constant with the same name cannot exist in different enumeration types. An example of the error is as follows:
Error statement 1: There is an enumeration type with the same name
typedef enum
{
    wednesday,
    thursday,
    friday
} workday;


typedef enum WEEK
{
    saturday,
    sunday = 0,
    monday,
} workday;
 
Error statement 2: There is an enumeration type with the same name cite member
typedef enum
{
    wednesday,
    thursday,
    friday
} workday_1;


typedef enum WEEK
{
    wednesday,
    sunday = 0,
    monday,
} workday_2;
 
 
3. Use variables
of enumeration type 3.1 Assign values ​​to variables of enumeration type.
The example compares the assignment of enumeration types with the assignment of basic data types:
Method 1: Declare the variable first, then assign the variable
#include<<stdio.h>




enum DAY { MON=1, TUE, WED, THU, FRI , SAT, SUN };


void main()
{
    
    int x, y, z;
    
    x = 10;
    y = 20;
    z = 30;
    
    
    enum DAY yesterday, today, tomorrow;
    
    yesterday = MON;
    today = TUE;
    tomorrow = WED;


    printf("%d %d %d \n", yesterday, today, tomorrow);
}
 
Method 2: Declare variables and assign initial values
​​#include <<stdio.h>




enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };


void main()
{
    
    int x=10, y=20, z=30;


    
    enum DAY yesterday = MON, 
                        today = TUE,
                   tomorrow = WED;


    printf("%d %d %d \n", yesterday, today, tomorrow);
}
 
Method 3: Declare the variable while defining the type, and then assign a value to the variable.
#include <<stdio.h>




enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN } yesterday, today, tomorrow;




int x, y, z;


void main()
{
    
    x = 10; y = 20; z = 30;
    
    
    yesterday = MON;
    today = TUE;
    tomorrow = WED;


    printf("%d %d %d \n", x, y, z); //output: 10 20 30
    printf("% d %d %d \n", yesterday, today, tomorrow); //Output: 1 2 3
}
 
Method 4: Type definition, variable declaration, and initial value assignment at the same time.
#include <<stdio.h>




enum DAY
{
    MON=1, 
    TUE,
    WED,
    THU,
    FRI,
    SAT,
    SUN 
}
yesterday = MON, today = TUE, tomorrow = WED;




int x = 10, y = 20, z = 30;


void main()
{
    printf("%d %d %d \n", x, y, z); //output: 10 20 30
    printf("%d %d %d \n", yesterday, today , tomorrow); //Output: 1 2 3
}
 
3.2 When assigning an integer value to an enumerated variable, type conversion is required.
#include <<stdio.h>


enum DAY { MON=1, TUE, WED, THU, FRI, SAT, SUN };


void main()
{
    enum DAY yesterday, today, tomorrow;


    yesterday = TUE;
    today = (enum DAY) (yesterday + 1); //type conversion
    tomorrow = (enum DAY) 30; //type conversion
    //tomorrow = 3; //error


    printf("%d %d %d \n", yesterday, today, tomorrow); //Output: 2 3 30
}
 
3.3 Using enum variables
#include<<stdio.h>


enum

    BELL = '\a',
    BACKSPACE = '\b',
    HTAB = '\t ',
    RETURN = '\r',
    NEWLINE = '\n', 
    VTAB = '\v',
    SPACE = ' '
};


enum BOOLEAN { FALSE = 0, TRUE } match_flag;


void main()
{
    int index = 0;
    int count_of_letter = 0;
    int count_of_space = 0;


    char str[] = "I'm Ely efod";


    match_flag = FALSE;


    for(; str[index] != '\0'; index++)
        if( SPACE != str[index] )
            count_of_letter++;
        else
        {
            match_flag = (enum BOOLEAN) 1;
            count_of_space++;
        }
    
    printf("%s %d times %c", match_flag ? "match" : "not match", count_of_space, NEWLINE);
    printf("count of letters: %d %c%c", count_of_letter, NEWLINE, RETURN);
}
 
输出:
match 2 times
count of letters: 10
Press any key to continue
 
4.Enumeration types and the sizeof operator
#include<<stdio.h>


enum escapes

    BELL      = '\a',
    BACKSPACE = '\b',
    HTAB      = '\t',
    RETURN    = '\r',
    NEWLINE   = '\n', 
    VTAB      = '\v',
    SPACE     = ' '
};


enum BOOLEAN { FALSE = 0, TRUE } match_flag;


void main()
{
    printf("%d bytes \n", sizeof(enum escapes)); //4 bytes
    printf("%d bytes \n", sizeof(escapes)); //4 bytes


    printf("%d bytes \n", sizeof(enum BOOLEAN)); //4 bytes
    printf("%d bytes \n", sizeof(BOOLEAN)); //4 bytes
    printf("%d bytes \n", sizeof(match_flag)); //4 bytes


    printf("%d bytes \n", sizeof(SPACE)); //4 bytes
    printf("%d bytes \n", sizeof(NEWLINE)); //4 bytes
    printf("%d bytes \n", sizeof(FALSE)); //4 bytes
    printf("%d bytes \n", sizeof(0)); //4 bytes
}
5. Comprehensive example
#include<<stdio.h>


enum Season //Note here if Put it below, you can't directly declare it like
{
    spring, summer=100, fall=96, winter
};


typedef enum
{
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
Weekday;


void main()
{
    
    printf( "%d \n", spring); // 0
    printf("%d, %c \n", summer, summer); // 100, d
    printf("%d \n", fall+winter); // 193


    Season mySeason=winter;                //就是这里,看上面的定义,也可以 enum Season mySeason
    if(winter==mySeason)
        printf("mySeason is winter \n"); // mySeason is winter
    
    int x=100;
    if(x==summer)
        printf("x is equal to summer\n"); // x is equal to summer


    printf("%d bytes\n", sizeof(spring)); // 4 bytes


    
    printf("sizeof Weekday is: %d \n", sizeof(Weekday)); //sizeof Weekday is: 4


    Weekday today = Saturday;
    Weekday tomorrow;
    if(today == Monday)
        tomorrow = Tuesday;
    else
        tomorrow = (Weekday) (today + 1); //remember to convert from int to Weekday
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325893080&siteId=291194637