Книга: Advanced PIC Microcontroller Projects in C
3.1.13 Enumerated Variables
3.1.13 Enumerated Variables
Enumerated variables are used to make a program more readable. In an enumerated variable, a list of items is specified and the value of the first item is set to 0, the next item is set to 1, and so on. In the following example, type Week is declared as an enumerated list and MON = 0, TUE = 1, WED = 2, and so on):
enum Week {MON, TUE, WED, THU, FRI, SAT, SUN};
It is possible to imply the values of the elements in an enumerated list. In the following example, black = 2, blue = 3, red = 4, and so on.
enum colors {black = 2, blue, red, white, gray};
Similarly, in the following example, black = 2, blue = 3, red = 8, and gray = 9:
enum colors {black = 2, blue, red = 8, gray};
Variables of type enumeration can be declared by specifying them after the list of items. For example, to declare variable My_Week of enumerated type Week, use the following statement:
enum Week {MON, TUE, WED, THU, FRI, SAT, SUN} My_Week;
Now we can use variable My_Week in a program:
My_Week = WED // assign 2 to My_Week
or
My_Week = 2 // same as above
After defining the enumerated type Week, we can declare variables This_Week and Next_Week of type Week as:
enum Week This_Week, Next_Week;
- 3.1.1 Comments
- 3.1.2 Beginning and Ending of a Program
- 3.1.3 Terminating Program Statements
- 3.1.4 White Spaces
- 3.1.5 Case Sensitivity
- 3.1.6 Variable Names
- 3.1.7 Variable Types
- 3.1.8 Constants
- 3.1.9 Escape Sequences
- 3.1.10 Static Variables
- 3.1.11 External Variables
- 3.1.12 Volatile Variables
- 3.1.13 Enumerated Variables
- 3.1.14 Arrays
- 3.1.15 Pointers
- 3.1.16 Structures
- 3.1.17 Unions
- 3.1.18 Operators in C
- 3.1.19 Modifying the Flow of Control
- 3.1.20 Mixing mikroC with Assembly Language Statements
- 3.1.10 Static Variables
- 3.1.11 External Variables
- 3.1.12 Volatile Variables
- Using Double Quotes to Resolve Variables in Strings with Embedded Spaces
- Using Environment Variables
- Perl Variables and Data Structures
- Special Variables
- Class and Object Variables
- Variables
- Creating Your Own Variables
- Using Variables in Shell Scripts
- Built-In Variables