Enumerations

Another type of variable that can be created is called an enumeration. An enumeration is a list of named integer constants. Thus, an enumeration type is simply a specification of the list of names that belong to the enumeration.

To create an enumeration requires the use of the keyword enum. The general form of an enumeration type is

enum enum-name { list of names } var-list;

The enum-name is the enumeration’s type name. The list of names is comma separated.

For example, the following fragment defines an enumeration of cities called cities and the variable c of type cities. Finally, c is assigned the value “Houston”.

enum cities { Houston, Austin, Amarillo} c; c = Houston; 

In an enumeration, the value of the first (leftmost) name is, by default, 0; the second name has the value 1; the third has the value 2; and so on. In general, each name is given a value one greater than the name that precedes it. You can give a name a specific value by adding an initializer. For example, in the following enumeration, Austin will have the value 10:

enum cities { Houston, Austin=10, Amarillo };

In this example, Amarillo will have the value 11 because each name will be one greater than the one that precedes it.




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net