About enumerations, when I used to write programs, almost unused, but occasionally in the other people wrote the driver Library has seen. Today I read the video of teacher Zhu, ready to summarize the enumeration related things.
One, about enumerations, you need to know
(1) In C language is a collection of some symbolic constants.
(2) The symbol in the enumeration is essentially a constant of type int.
(3) When using enumerations, we tend to focus on the symbol itself, not the value of the symbol.
(4) Enumerations can enclose some associated symbols in an enumeration, which is a multiple-choice one.
Second, Code practice operation
"Example 1": Enumeration definition (Method 1)
1#include <stdio.h>2 3 //Enumeration Definition: Method One4 enumWeek5 {6 SUN,7 MON,8 TUE,9 WEN,Ten THU, One FRI, A SAT, - }; - the intMainvoid) - { - //Test: Method One - enumWeek today;//Statement +Today =MON; -printf"today =%d.\n", today);//The result is: Today = 1. + A return 0; at}
Results:
Analysis: It is possible to use Mon instead of "Today" for enumeration. MON ", it also indicates that the symbol in the enumeration is not defined in another symbol.
"Example 2": Enumeration definition (Method 2)
1 // 2 enum week 3 { SUN, 5 MON, TUE, 7 WEN, 8 THU, 9 FRI, 10 SAT, 11 }today,yesterday;
Description: Use the same method as above.
"Example 3": Enumeration definition (Method 3)
1 // enum 3 { 4 SUN, MON, 6 TUE, 7 WEN, 8 THU, 9 FRI, 10 SAT, 11 }today,yesterday;
Description: See here is not think of the structure, example 3 and Example 2 different is no week, but for the enumeration of the use of today is the same. Do not think that there is nothing behind the enum is wrong, so the definition is correct.
"Example 4": Enumeration definition (method 4) The involvement of a typedef
1 //Enumeration Definition: Method four2typedefenumWeek3 {4 SUN,5 MON,6 TUE,7 WEN,8 THU,9 FRI,Ten SAT, One }wk; A - intMainvoid) - { the //Test: Method Four - WK today; -Today =SAT; -printf"today =%d.\n", today); + - return 0; +}
Results:
Analysis: By combining the above program with the typedef of the learned structure, you will find the definition of an enumeration (wk today; This is a good understanding).
Note: The above procedures are based on "teacher Zhu's Internet of Things video" in the reference, hereby affirm.
C Language Notes (enumeration)