3.1 Enumeration
3.2 Structure
3.3 Type definition
3.1 Enumeration
An enumeration is a user-defined data type that is indicated by the keyword enum in the following syntax:
Enum Enum type name {name 0,..., name n};
Enumeration type names are usually not really used, but they are used in curly braces, because they are constant symbols, they are of type int, and the values are from 0 to n sequentially. Such as:
Enum colors{red, yellow, green};
Three constants are created, and the value of red is the value of 0,yellow is 1, and the value of green is 2
When you need some constant values that can be arranged, the meaning of the definition enumeration is to give the names of these constant values
Routines: enumeration of automatic counts
This is convenient when you need to iterate through all the enumerators or need to create an array that uses an enumerator to do the subscript.
1 #define_crt_secure_no_warnings2 3#include <stdio.h>4 5 enumColors6 {7Red, yellow, green, numcolors//enumeration of routines, the last number of colors to put8 };9 Ten Main () One { A intcolor =-1; - Char*colorsnames[numcolors] = {"Red","Yellow","Green" }; - Char*colorname =NULL; the -printf"Enter the code for the color you like:"); -scanf"%d", &color); - + if(Color >=0&& Color <numcolors) - { +ColorName =Colorsnames[color]; A } at Else - { -ColorName ="Unknown"; - } - -printf"The color you like is%s\n.", colorname); in -System"Pause"); to}
Enumeration Amount
You can specify a value when declaring an enumerator
1 enum Colors 2 {3 1 5 4 };
Enum is just int
Even if you assign an integer value that does not exist to a variable of the enumerated type, there is no warning or error
Although enumeration types can be used as types, they are actually (BU) less (hao)
If there is a meaningful name for parallelism, it is easier to use enumerations than const INT
Enumeration Bihon (macro) Good, because enumerations have int types
3.2 Structure
3.3 Type definition
NetEase Cloud Classroom _c Language programming third week: Structure: structure, type definition, union