C enumeration and struct-body
This article turns from CSDN's Vjjjjjta
In real-world applications, there are only a few possible variables to be evaluated. If there are only two possible values for a person's sex, there are only seven possible values to be taken in the week. Variables that are more specific to such values in the C language can be defined as enumerated types. The so-called enumeration refers to the value of the variable is enumerated, the variable is limited to the value enumerated in the range of values.
Defining a variable is an enumeration type, you can define an enumeration type name first, and then explain that the variable is the enumeration type.
For example:
Enum Weekday{sun,mon,tue,wed,thu,fri,sat};
An enumeration type name, enum weekday, is defined, and then the variable is defined as the enumeration type. For example:
Enum weekday day;
Of course, you can also define enum type variables directly. For example:
Enum Weekday{sun,mon,tue,wed,thu,fri,sat} day;
Where Sum,mon,..., sat are called enumeration elements or enumeration constants, which are user-defined identifiers.
The following points need to be explained.
The ① enumeration element is not a variable, but a constant, so an enumeration element is also known as an enumeration constant. Because it is a constant, you cannot assign a value to an enumeration element.
② enumeration elements as constants, they are valued, and C languages are compiled with their values in the defined order,...。
In the above description, Sun has a value of 0,mon of 1,...sat with a value of 6, if there is an assignment statement
Day=mon;
The value of the day variable is 1. Of course, the value of this variable can be output. For example:
printf ("%d", day);
The integer 1 will be output.
If you specify the value of an element when defining an enumeration type, you can also change the value of the enumeration element. For example:
Enum Weekday{sun=7,mon=1,tue,wed,thu,fri,sat}day;
At this time, Sun for 7,mon 1, after the element is added 1, so the SAT is 6.
③ enumeration values can be used for judgment. For example:
if (Day==mon) {...}
if (Day>mon) {...}
The comparison rule for enumeration values is that the value of the first enumeration element is considered 0 when it is compared by its ordinal number in the description, and if no one is specified. For example, Mon>sun,sat>fri.
C language tutorial? 216?
④ An integer cannot be assigned directly to an enumeration variable, a type conversion must be forced to assign a value. For example:
Day= (enum weekday) 2;
This assignment means that the enumeration element with order number 2 is assigned to day, which is equivalent to Workday=tue;
Example 11.6 enters an integer from the keyboard that displays the English name of the enumeration constant corresponding to the integer.
# include
void Main ()
{
Enum weekday {Sun,mon,tue,wed,thu,fri,sat} day;
int k;
printf ("Input a number (0--6)");
scanf ("%d", &k);
Day= (enum weekday) K;
Switch (day)
{
Case sun:printf ("sunday/n");
Case mon:printf ("monday/n");
Case tue:printf ("tuesday/n");
Case wed:printf ("wednesday/n");
Case thu:printf ("thursday/n");
Case fri:printf ("friday/n");
Case sat:printf ("satday/n");
default:printf ("Input error/n");
}
}
The result of the program operation is:
Input a number (0--6) 1
Monday
In this program, enumeration constants can be compared with enumeration variables, but to output an English word that corresponds to an enumeration constant, you cannot use the following statement:
printf ("%s", Mon);
Because the enumeration constant, Mon, is an integer value, not a string.
When using an enumeration variable, the primary concern is not the size of its value, but the state it represents.
------------------------------------------------------------------
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;
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 The types of variables x, y, and z are int integers
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.
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
} workday; The type of the variable workday is enum-enum day
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
{
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
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
{
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 <stdio.h>
/* defines enumeration type */
enum day { mon=1, tue, wed, thu, fri, sat, sun };
Void main ()
{
/* declare the variable using the base data type, and then assign a value to the variable */
int x, y, z;
x = 10;
y = 20;
z = 30;
/* declare a variable with an enumeration type, and then assign a value to the enumerated variable */
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 <stdio.h>
/* Define enum type */
Enum Day {mon=1, TUE, WED, THU, FRI, SAT, SUN};
void Main ()
{
/* Use basic data type to declare variables and assign initial values to variables */
int x=10, y=20, z=30;
/* Declare a variable with an enumeration type and assign an initial value to the enumerated variable simultaneously */
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 <stdio.h>
/* Define an enumeration type and declare three variables of that type, all of which are global variables */
Enum Day {mon=1, TUE, WED, THU, FRI, SAT, SUN} Yesterday, today, tomorrow;
/* Define three variables with basic data types, all of which are global variables */
int x, y, Z;
void Main ()
{
/* Assign a variable to the base data type */
x = 10; y = 20; z = 30;
/* Assign values to enumerated variables */
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 <stdio.h>
/* Define an enumeration type, declare three variables of that type, and assign an initial value. All of them are global variables */
Enum Day
{
Mon=1,
TUE,
WED,
THU,
FRI,
Sat
SUN
}
Yesterday = MON, today = TUE, tomorrow = WED;
/* Define three variables with a basic data type and assign an initial value. All of them are global variables */
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 <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 conversions
Tomorrow = (enum day) 30; Type conversions
Tomorrow = 3; Error
printf ("%d%d%d/n", Yesterday, today, tomorrow); Output: 2 3 30
}
3.3 Using the enum-type variable
#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);
}
Output:
Match 2 times
Count of Letters:10
Press any key to continue
4. Enumeration type and 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. General examples
#include <stdio.h>
Enum Season
{
Spring, summer=100, fall=96, winter
};
typedef enum
{
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}
Weekday;
void Main ()
{
/* Season */
printf ("%d/n", spring); 0
printf ("%d,%c/n", Summer, Summer); The D
printf ("%d/n", Fall+winter); 193
Season Myseason=winter;
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
/* Weekday */
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
}
Click to view original: http://blog.csdn.net/vjjjjjta/article/details/6337081
Enumeration of C (GO)