10 Enumerated types

Enumerated types are handy if you just want to use integer values to represent choices and not numerical values. For example, you can use enumerated types to represent days in a week. The following is the definition of such a type:

enum dow { sun, mon, tue, wed, thu, fri, sat };

Given this type definition, you can create variables of this type:

enum dow today;

Then, you can assign values to today:

today = tue;

Note that C allows you to assign any integer values to an enum type, which can lead to much abuse. As a programming practice, you should only use the symbolic terms defined for an enumerated type. In our example, this means we should only use sun to sat when we specify values in expressions involving a enum dow type.



Copyright © 2006-09-25 by Tak Auyeung