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-list is a comma-delimited list of discreet values, enumerators (or enumeration constants). Each enumerator 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 PIC32, 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

Initializer expression can include previously declared enumerators. For example, in the following declaration:

enum memory_sizes { bit = 1, nibble = 4 * bit, byte = 2 * nibble,
                    kilobyte = 1024 * byte };

nibble would acquire the value 4, byte the value 8, and kilobyte the value 8192.

Anonymous Enum Type

In our previous declaration, the identifier colors is an optional enumeration tag that can be used in subsequent declarations of enumeration variables of the enum colors type:

enum colors bg, border;  /* declare variables bg and border */

Like with struct and union declarations, you can omit the tag if no further variables of this enum type are required:

/* Anonymous enum type: */
enum { black, red, green, blue, violet, white } color;

Enumeration Scope

Enumeration tags share the same name space as structure and union tags. Enumerators share the same name space as ordinary variable identifiers:

int blue = 73;

{ // open a block
   enum colors { black, red, green, blue, violet, white } c;
   /* enumerator blue = 3 now hides outer declaration of int blue */

   struct colors { int i, j; };   // ILLEGAL: colors duplicate tag
   double red = 2;                // ILLEGAL: redefinition of red
}

blue = 37;                        // back in int blue scope
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code