Using enumerated item values as literal integer constants
There is one other form of enumerated types. It is when we want to give an identifier to a literal constant value and we want to avoid the use of #define
. Early versions of C relied upon the preprocessor heavily – later versions, much less so. The pros and cons of using the preprocessor will be discussed in Chapter 24, Working with Multi-File Programs.
Instead of using #define
, we can declare an anonymous enumerated type that contains enumerated items that act identically to literal constants, as follows:
enum {
inchesPerFoot = 12,
FeetPerYard = 3,
feetPerMile = 5280,
yardsPerMile = 1760,
...
}
Note that there is no name associated with the enum
type; this is what makes it anonymous. Each of these enumerated values can be used wherever we need that value; we simply use its name. This will...