6.5. Enums

These fall into the category of ‘half baked’. They aren't proper enumerated types, as in Pascal, and only really serve to help you reduce the number of #define statements in your program. They look like this:

enum e_tag{
      a, b, c, d=20, e, f, g=20, h
}var;

Just as with structures and unions, the e_tag is the tag, and var is the definition of a variable.

The names declared inside the enumeration are constants with int type. Their values are these:

a == 0
b == 1
c == 2
d == 20
e == 21
f == 22
g == 20
h == 21

so you can see that, in the absence of anything to the contrary, the values assigned start at zero and increase. A specific value can be given if you want, when the increase will continue one at a time afterwards; the specific value must be an integral constant (see later) that is representable in an int. It is possible for more than one of the names to have the same value.

The only use for these things is to give a better-scoped version of this:

#define a 0
#define b 1
/* and so on */

It's better scoped because the declaration of enumerations follows the standard scope rules for C, whereas #define statements have file scope.

Not that you are likely to care, but the Standard states that enumeration types are of a type that is compatible with an implementation-defined one of the integral types. So what? For interest's sake here is an illustration:

enum ee{a,b,c}e_var, *ep;

The names a, b, and c all behave as if they were int constants when you use them; e_var has type enum ee and ep is a pointer to enum ee. The compatibility requirement means that (amongst other implications) there will be an integral type whose address can be assigned to ep without violating the type-compatibility requirements for pointers.