Книга: 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;

Оглавление книги


Генерация: 0.988. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз