Datasheet
ENUMERATIONS
An enumeration data type is used for representing an abstract, discreet set of val-
ues with appropriate symbolic names.
Enumeration Declaration
Enumeration is declared like this:
enum tag {enumeration-list};
Here, tag is an optional name of the enumeration; enumeration-list is a comma-
delimited list of discreet values, enumerators (or enumeration constants). Each enu-
merator is assigned a fixed integral value. In the absence of explicit initializers, the
first enumerator is set to zero, and the value of each succeeding enumerator is set
to a value of its predecessor increased by one.
Variables of the enum type are declared the same as variables of any other type.
For example, the following declaration:
enum colors { black, red, green, blue, violet, white } c;
establishes a unique integral type, enum colors, variable c of this type, and set of
enumerators with constant integer values (black = 0, red = 1, ...). In the mikroC PRO
for AVR, a variable of an enumerated type can be assigned any value of the type
int – no type checking beyond that is enforced. That is:
c = red; // OK
c = 1; // Also OK, means the same
With explicit integral initializers, you can set one or more enumerators to specific
values. The initializer can be any expression yielding a positive or negative integer
value (after possible integer promotions). Any subsequent names without initializers
will be increased by one. These values are usually unique, but duplicates are legal.
The order of constants can be explicitly re-arranged. For example:
enum colors { black, // value 0
red, // value 1
green, // value 2
blue=6, // value 6
violet, // value 7
white=4 }; // value 4
152
MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Language Reference
mikroC PRO for AVR
CHAPTER 5