This is an old revision of the document!
The notion of enumeration type was added to the C language in the ANSI C standard, first published in 1989, after the C language had already been in widespread use for a decade. The variable declaration
enum { kCat, kDog, kCow, kHorse } animal;
declares an integer variable animal
, which is supposed to have only four permissible values, identified by the symbolic constant names kCat
, kDog
, kCow
, and kHorse
. It's common to see enum
used together with typedef
, to define an “enum type” (especially in a .h
header file) which can subsequently be used to declare any number of variables with the same set of permissible values, e.g.
typedef enum { kCat, kDog, kCow, kHorse } Animal; ... Animal rover, spot, dobbin;
This looks very nice, and it's tempting to think that we have succeeded in defining a truly new data-type, but the reality is quite different. The problems are suggested by the very words I just used: