Original: http://z515256164.blog.163.com/blog/static/32443029201192182854300/
Have changed
C-language explanation-enum type
Note: The following all code execution Environment is VC + + 6.0
In a program, you might need to define an alias for some integers, and we can do this with the preprocessing directive # define, and your code might 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 type of data, and hopefully it will do the same. This new data type is called enum type.
1. Define A new data type-enum type
The following code defines this new data type-enum type
Enum Day
{
Mon=1, TUE, WED, THU, FRI, SAT, SUN
};
(1) An enum is a collection in which the elements (enumeration members) are named integer constants, separated by commas.
(2) Day is an identifier that can be regarded as the name of the collection, an optional, or optional item.
(3) The default value of the first enumeration member is 0 of the integer type , and the value of the subsequent enumeration member is added 1 on the previous member.
(4) The value of an enumeration member can be set artificially to customize an integer within a range.
(5) Enum type is an alternative to the pre-processing directive # define.
(6) The type definition is semicolon-terminated;
Deep Anatomy of C language:
The general definition is as follows:
Enum Enum_type_name
{
Enum_const_1,
Enum_const_2,
...
Enum_const_n
} enum_variable_name;
Note: Enum_type_name is a custom data data type name , and Enum_variable_name is a variable of type enum_type_name, which is the enumeration variable we usually say. In fact, the Enum_type_name type is the qualification of the value range of a variable, and the curly brace is its range of values, that is, the variable enum_variable_name of the Enum_type_name type can only be evaluated as any one of the values in curly braces. If the value assigned to the type variable is not in the list, an error or warning is given. Enum_const_1, enum_const_2 、...、 Enum_const_n, these members are constants, which is what we normally call enumeration constants ( constants are generally capitalized ).
2. Declaring a variable with an enumeration type
Once the new data type definition is complete, it is ready to use. We have seen the most basic data types, such as Integer int, single-precision float float, double-precision floating-point double, char char, short integer, and so on. Declaring variables with these basic data types is usually the case:
Char A; The type of variable A is character char
Char letter;
int x, y, z;//variables x, y, and z are of int integer type
int number;
Double m, N;
Double result; The type of the variable result is a double-precision floating-point type
Since an enumeration is also a data type, it can declare variables as well as basic data types.
Method One: The definition of the enumeration type and the declaration of the variable are separated
Enum Day
{
Mon=1, TUE, WED, THU, FRI, SAT, SUN
};
Enum Day yesterday;
Enum Day Today;
Enum Day tomorrow; The type of the variable tomorrow is enum-enum day
Enum Day Good_day, Bad_day; The types of variables Good_day and BAD_DAY are enumerated enum day
Method Two: Type definition and variable declaration at the same time:
enum//differs from the first definition, where the designator day is omitted, which is allowed. This approach is equivalent to defining the
{ An enumeration variable, reference
Saturday, above C language depth anatomy definition
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
} workday; The type of the variable workday is enum-enum day
Workday=friday;
sizeof (WORKDAY) = 4;
Enum Week {mon=1, Tue, Wed, Thu, Fri Sat, Sun} days; The type of the variable days is enumerated enum week
Enum BOOLEAN {false, true} End_flag, Match_flag; Defines an enumeration type and declares two enum-type variables
Method Three: Use the TypeDef keyword to define an enumeration type as an alias and use that alias for variable declaration:
typedef enum Workday//Workday here can be omitted, or changed to other, will not affect the back
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
} workday; //The workday here is an alias of enum Enum workday, similar to int
Workday today, tomorrow;///Here Workday The colors correspond to the types of the variables today and tomorrow are enumerated type workday, which is enum workday
The workday in enum workday can be omitted:
typedef enum
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
} workday; The workday here is the alias of enum enum workday
Workday today, tomorrow; Variables today and tomorrow are enumerated type workday, which is enum workday
You can also use this method:
typedef enum WORKDAY //vc6.0 and MDK compilation failed
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
};
Workday today, tomorrow; Variables today and tomorrow are enumerated type workday, which is enum workday
Note: An enumeration type with the same name cannot be defined in the same program, and a named constant with the same name cannot exist in a different enumeration type. The error examples are as follows:
Error declaration One: An enumeration type with the same name exists
typedef enum
{
Wednesday,
Thursday,
Friday
} workday;
typedef enum WEEK
{
Saturday
Sunday = 0,
Monday
} workday;
Error declaration two: An enumeration member with the same name exists
typedef enum
{
Wednesday ,
Thursday,
Friday
} workday_1;
typedef enum WEEK
{
Wednesday ,
Sunday = 0,
Monday
} workday_2;
3. Using variables of enum type
3.1 Assigns a value to the enumeration type variable.
An instance compares the assignment of an enumeration type with the assignment of a base data type:
Method One: Declare the variable first, then assign a value to the variable
#include
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 Two: Assigning the initial value while declaring the variable
#include
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 Three: Declare the variable while defining the type, and then assign a value to the variable.
#include
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 Four: Type definition, variable declaration, assign the initial value at the same time.
#include
Enum Day
{
Mon=1,
TUE,
WED,
THU,
FRI,
Sat
SUN
}
Yesterday = MON, today = TUE, tomorrow = WED;
int x = ten, y =, 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, a type conversion is required.
#include
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 conversions
Tomorrow = (enum day) 30; Type conversions
Tomorrow = 3; Error
printf ("%d%d%d \ n", Yesterday, today, tomorrow); Output: 2 3 30
}
Problem:
Enum Color
{
GREEN = 1,
RED,
BLUE,
green_red = 10,
Green_blue
}colorval;
1. Enumeration can do things, #define宏能不能都做到? If so, why do I need to enumerate?
2. Seeking sizeof (Colorval)Enumerations can be self-increment by 1, so that each value is not defined, and a macro must be defined for each value. And an enumeration is a collection that represents a class of values, such as the color in your code, which is easy to use, and macros cannot form a collection.
sizeof (Colorval) is 4 because Colorval is an enumeration variable, and the enumeration variable represents an integer (such as Colorval = RED), and the integer is 4 bytes.
C language--enum,typedef enum enum type