In ANSI C
, you cannot rely on a variable of an enum type being the same as an int
. The C standard is not publicly available but you can find some information about this on the web(1): ."An enumeration type is compatible with some integral type". This does not necessarily imply int, although some compilers might define it this way. It also doesn't mean that all ints are the same size.
Imagine if all enum types were ints. If ints are 32 bit, then it is impossible to define an enumeration with 64-bit values, for example. There is also the question of signedness - if an enum is an int, is it a signed or unsigned int? If it is signed, you cannot have an enumeration value equal to UINT_MAX. If an unsigned int, you cannot have negative values. Any restrictions like these would limit the usefulness of enumerated types. It therefore makes sense that this is an implementation-defined behavior.
Of course, it is the case that in some compilers, enums are stored as ints. You can't rely on this being the case everywhere, however.