Enumeration
what is an enumeration.
An enumeration is a user-defined data type that is declared with a keyword enum as follows: Enum enumeration type name {Name 0, ..., name n};
In short, it is to enumerate the possible results of a thing.
how to use enumerations.
Here's an example
Example 1:
# include <stdio.h>
enum weekday {
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
};< C6/>int Main (void) {
enum weekday day1, Day2;
Day1 = Monday;
Day2 = Sunday;
printf ("Monday =%d\n", day1);
printf ("Sunday =%d\n", day2);
return 0;
}
The output results are:
To be explained, the 3rd to 5th line of code in this program, just defines a data type, does not define a variable, the data type is enum weekday
The output is 0 and 6, because the elements in the enumeration are incremented from 0, and, of course, if you specify the value of the element, the elements that follow will continue to increment sequentially, as shown below
Example 2:
# include <stdio.h>
enum Weekday {
Monday = 1, Tuesday, Wednesday = 7, Thursday, Friday, Saturday, Sunday
};
int main (void) {
enum weekday day1, Day2, Day3, Day4;
Day1 = Monday;
Day2 = Tuesday;
Day3 = Wednesday;
Day4 = Sunday;
printf ("Monday =%d\n", day1);
printf ("Tuesday =%d\n", day2);
printf ("Wednesday =%d\n", day3);
printf ("Sunday =%d\n", day4);
return 0;
The output results are:
Why do you use enumerations?
Before you answer this question. The first thing to say is a constant notation, the so-called constant notation, that is, symbols rather than specific numbers to represent the numbers in the program, this can use the const and define implementations, but the const and define are relatively cumbersome, and enumerations here some convenient place, Because enumerated elements are generally treated as constants
If the amount of an enum in Example 1 is expressed using a const or define, then
int const Monday = 0;
int const Tuesday = 1;
int const Wednesday = 2;
int const Thursday = 3;
int const FRIDAY = 4;
int const SATURDAY = 5;
int const SUNDAY = 6;
Or
# define Monday 0
# define Tuesday 1
# define Wednesday 2
# define Thursday 3
# define Friday 4
# Defi NE Saturday 5
# define Sunday 6
Enumerated type names are often not actually used, using names in curly braces because they are constant symbols, their type is int, and the value is 0 to N in turn. such as: enum colors {red, yellow, green}; Three constants were created, Red is 0,yellow is 1,⽽ and green is 2. When you need to ⼀ some constant values that can be sorted, the meaning of defining enumerations is to give these constant value names.
Enumeration types can be followed by an enum as a type, but are actually the pros and cons of an enumeration of internal computations and external input output with integers
Although an enumeration type can be used as a type to ⽤, it is rarely (bad) used, and if the name is in the sense of parallelism, it is convenient to enumerate the Bihon (macro) than the const int, because the enumeration has an int type
If you want to count the enumeration elements, you can add a number of useful elements after the normally defined enumeration element, for example, enum color{red, yellow, green, blue, purple, Num};,num with a value of 5, It can represent the number of valid elements in this enumeration, which can be used when iterating over the enumeration elements (also called a common body) .
The keyword is union, all its members share a single memory space, only one member is valid at the same time, the size of which is the memory of the largest member of the memory
The format is similar to the structure, so it can be defined as
Union data {
int i;
char c;
} A, B, C;
You can also do this--
Union data {
int i;
char c;
};
Union data A, B, C;
You can also do this--
Union {
int i;
char c;
} A, B, C;
This is the same type as the surface of the structural body
Let me give you a few examples
Example 3:
# include <stdio.h>
Union data {
int i;
char c;
} A;
int main (void) {
a.i = 3;
A.C = ' A '; The ASCLL value of A is
("%d\n", A.I);
printf ("A.I's address is:%P\NA.C's address is:%p\n", &A.I, &A.C);
return 0;
The output results are:
From the results can better understand the above words, the final output is A.C, so that at the same time only one member is valid, and the final output of the Union data and A.I address the same, also proved that "its size is the memory of the largest member of the memory" This sentence is correct
Also, a consortium cannot be used as a parameter to a function, nor can it be the return type of a function, which is not the same as a struct, but you can still use a pointer to a federation variable (similar to the usage of a struct variable), and it can also appear in the structure, and you can define a federation array, of course, Structural bodies and arrays can also appear in the consortium
Example 4:
# include <stdio.h>
# include <stdlib.h>
Union data {
int i;
char c;
} A, * p;
int main (void) {
int s, k = 1;
char ch = ' A ';
Union data t[3];
p = (Union data *) malloc (sizeof (Union data));
P->i = ten;
p->c = ' A ';
For (s=0 s<3; s++) {
t[s].i = k++;
T[S].C = ++ch;
}
For (s=0 s<3; s++) {
printf ("%d", t[s].c);
}
return 0;
}
The output results are:
14, 15, 16 lines do not complain
"All code runs through the dev C + + under Windows System"
(If there is a mistake, please correct me)