enum Enumeration Data Type
An enumeration defines a set of named integer constants. At any given
time, a variable of an enumeration type can contain any one of its
set of integer constants. An enum is stored as a signed int. An
enum type is optionally named using a tag. An enum declaration has
the form:
enum [tag] {enum-list} declarator [, declarator ...] ;
enum-list has the form
identifier [= cex][, identifier [= cex]] ...
where cex represents a constant integer expression. Once an enum set
has been defined, it may be referenced in subsequent declarations
using the format
enum tag declarator [, declarator ...] ;
Notes: If an enum set has no tag, it cannot be referenced in
subsequent (and separate) declarations, casts, or
function definitions.
Enums with different tag names define different data
types, even if their layouts are identical. That is, such
enums are not assignment-compatible and should not be
copied to or compared with each other. If you really mean
two enum types to be the same, make them the same. This
also applies to enums without tags.
Unless otherwise specified, the first member in a set is
assigned the value 0, the next 1, the next 2, etc. You
may assign member values specifically as shown in the
example of enum hair_color (see the brunette in the last
example below). Any member without an explicitly assigned
value will have a value one greater than the previous
member's value. As shown in the examples, members may
have duplicate values. There may also be gaps in the
range of values assigned.
Each enum has its own identifier namespace, and an enum
tag must be distinct from other enum, struct, and union
tags.
Since an enum is really a fancy int, the language permits
you to deal with enums as ints in all cases, allowing
such things as car_color = 26; if (car_color ==
-4)...etc. Such practices are discouraged.
-------------------------------- Example ---------------------------------
enum color {red, green, blue, black} car_color;
enum color flower_color, *ptr_color, colors[10];
car_color = green;
if (car_color == red) ...
enum hair_color {blonde = -2, redhead = -1, brown = -2,
brunette};
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster