
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>enum enumeration data type</b>
[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
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 redhead 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, and if maximum
warning mode is enabled at compile-time (via the /W
switch), you will be warned about the logical mismatch.
A static or external enum will be initialized to zero, by
default, since it is an int. However, since zero may not
be the value of any of the members in the set, you should
specifically initialize enums before using them.
-------------------------------- 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};
See Also: struct union
Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson