Enumeration Learning:
Enum Weekday{sun,mon,tue,wed,thu,fri,sat};
Enum weekday day;
can also be:
Enum Weekday{sun,mon,tue,wed,thu,fri,sat} day;
If you have an assignment statement:
Day=mon;
The day variable has a value of 1.
Enum Weekday{sun=7,mon=1,tue,wed,thu,fri,sat}day;
At this point, sun for 7,mon 1, after the elements in sequence plus 1, so the SAT is 6.
③ enumeration values can be used for judgment. For example:
if (Day==mon) {...}
if (Day>mon) {...}
④ An integer cannot be assigned directly to an enumeration variable, you must force a type conversion to assign a value. For example:
Day= (enum weekday) 2;
In this program, the enumeration constants can be compared with the enumeration variables, but to output the English words corresponding to the enumerated constants, you cannot use the following statement:
printf ("%s", Mon);
Because enumerated constants are Mon As integer values, not strings.
When using an enumeration variable, the primary concern is not the size of its value, but the state it represents.
Enum Day
{
Mon=1, TUE, WED, THU, FRI, SAT, SUN
};
(1) An enumerated type is a collection in which elements (enumeration members) are named integer constants, separated by commas.
(2) Day is an identifier that can be viewed as the name of the set, an optional, or optional item.
(3) The default value of the first enumeration member is 0 of the integral type, and the subsequent enumeration member's value is 1 on the previous member.
(4) The value of an enumeration member can be set artificially, thereby customizing a range of integers.
(5) The enumeration type is the substitution of preprocessing instruction #define.
(6) type definition with semicolon;
Since an enumeration is also a data type, it can declare a variable as well as a basic data type.
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 variable tomorrow is enum type enum day
Enum Day Good_day, Bad_day; Variable good_day and Bad_day types are enumerated enum day
Method Two: The type definition is concurrent with the variable declaration:
Enum//Is different from 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 enum type enum day
Enum Week {mon=1, Tue, Wed, Thu, Fri Sat, Sun} days; The type of the variable days is enum-type enum week
Enum BOOLEAN {false, true} End_flag, Match_flag; Defines an enumeration type and declares two enumerated variables
typedef enum WORKDAY
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
} workday; The workday here is an alias for an enumerated enum workday
Workday today, tomorrow; The variables today and tomorrow are enumerated type workday, or enum workday
The workday in the enum workday can be omitted:
typedef enum
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
} workday; The workday here is an alias for an enumerated enum workday
Workday today, tomorrow; The variables today and tomorrow are enumerated type workday, or enum workday
You can also use this method:
typedef enum WORKDAY
{
Saturday
Sunday = 0,
Monday
Tuesday,
Wednesday,
Thursday,
Friday
};
Workday today, tomorrow; The variables today and tomorrow are enumerated type workday, or enum workday
Error declaration One: there is an enumerated type with the same name
typedef enum
{
Wednesday,
Thursday,
Friday
} workday;
typedef enum WEEK
{
Saturday
Sunday = 0,
Monday
} workday;
Error declaration two: An enumeration member with the same name
typedef enum
{
Wednesday,
Thursday,
Friday
} workday_1;
typedef enum WEEK
{
Wednesday,
Sunday = 0,
Monday
} workday_2;
Method One: Declare the variable first, then assign a value
#include <stdio.h>
/* Define the enumeration type */
Enum Day {mon=1, TUE, WED, THU, FRI, SAT, SUN} ;
Void Main ()
{
/* Declares variables using basic data types, and then assigns values to variables */
int x, y, z;
&nb sp;
x = ten
y =;
z = n
&n Bsp
/* Use enumerated types to declare variables, and then assign values to enumerated variables */
enum Day yesterday, today, tomorrow;
yesterday = MON;
today = TUE;
& nbsp; tomorrow = WED;
printf ("%d%d%d/n", Yesterday, today, tomorrow);
}
Method Two: Declare a variable with an initial value
#include <stdio.h>
/* Define enum type */
Enum Day {mon=1, TUE, WED, THU, FRI, SAT, SUN};
Void Main ()
{
/* uses basic data types to declare variables and to assign an initial value to a variable */
int x=10, y=20, z=30;
/* Declare variables with enumerated types and assign an initial value to an enumerated variable */
enum Day yesterday = MON,
&NBSP;&NBSP;&NBSP;&N bsp; today = TUE,
tomorrow = WED;
printf ("%d%d%d/n", Yesterday, today, tomorrow);
}
The
#include <stdio.h>
/* Defines an enumeration type and declares three variables of that type, all of which are global variables */
Enum Day {mon=1, TUE, WED, THU, FRI, SAT, SUN Yesterday, today, tomorrow;
/* Defines three variables with basic data types, all of which are global variables */
int x, y, z;
Void Main ()
{
/* Assign values to a variable of the base data type */
& nbsp; x = 10; y = 20; z = 30;
/* Assign values to enumerated variables */
yesterday = MON
& nbsp today = TUE;
tomorrow = WED;
printf ("%d%d%d/n", X, Y, z);//output:
printf ("%d%d%d/n", Yesterd Ay, today, tomorrow); Output: 1 2 3
}
Method Four: type definition, variable declaration, and initial assignment. The
#include <stdio.h>
/* Defines an enumeration type, declares three variables of that type, and assigns an initial value. They are global variables */
Enum day
{
mon=1,
TUE,
WED,
THU,
FRI,
SAT,
SUN
}
Ye Sterday = MON, today = TUE, tomorrow = WED;
/* Defines three variables with the base data type and assigns an initial value. They are global variables */
int x = ten, y =, z = =
void Main ()
{
printf ("%d%d%d/n", X, Y, z); /output:
printf ("%d%d%d/n", Yesterday, today, tomorrow);/output: 1 2 3
}
3.2 When assigning an integer value to a variable of an enumerated type, 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 enumerated 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);
}
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. Comprehensive 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); MB, 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
}