6.5. EnumsThese 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
enum e_tag{
a, b, c, d=20, e, f, g=20, h
}var;
Just as with structures and unions, the The names declared inside the enumeration are constants with
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 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 |
The C BookThis book is published as a matter of historical interest. Please read the copyright and disclaimer information. GBdirect Ltd provides up-to-date training and consultancy in C, Embedded C, C++ and a wide range of other subjects based on open standards if you happen to be interested. |
|
West Yorkshire Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday South East Regional Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday Please note: |