mikroC

mikroC - C Compiler for Microchip PIC microcontrollers

making it simple...

Enumerations

An enumeration data type is used for representing an abstract, discreet set of values 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-listis a list of discreet values, enumerators. The enumerators listed inside the braces are also known as enumeration constants. Each is assigned a fixed integral value. In the absence of explicit initializers, the first enumerator is set to zero, and each succeeding enumerator is set to one more than its predecessor.

Variables of enum type are declared 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, colors, a variable c of this type, and a set of enumerators with constant integer values (black = 0, red = 1, ...). In C, a variable of an enumerated type can be assigned any value of 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 then increase by one. These values are usually unique, but duplicates are legal.

page

 

62

MikroElektronika: Development tools - Books - Compilers