Enumerations, Mikroc – ABL electronic PIC Microcontrollers PIC16 User Manual

Page 70

Advertising
background image

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 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 suc-
ceeding 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 initializ-
ers will then increase by one. These values are usually unique, but duplicates are
legal.

mikroC - C Compiler for Microchip PIC microcontrollers

mikroC

making it simple...

62

MikroElektronika: Development tools - Books - Compilers

page

Advertising
This manual is related to the following products: