Introducing enumerations
There are times when we want a program or function variable to take an integer value that is from a set of related values. For convenience, and to make the relationship between each value clear, each value in the set of values is given a name. We can think of this set as a grouping of conceptually related values.
Let's say we want a variable to represent the suits of a deck of cards. Naturally, we know each suit by its name – spades, hearts, clubs, and diamonds. But C doesn't know about card names or card suits. If we wanted to represent each of these suits with a value, we could pick any value for each, say, 4
for spades, 3
for hearts, 2
for diamonds, and 1
for clubs. Our program, using this simple scheme, might look as follows:
int card;
...
card = 3; // Heart.
But we would have to do the work of remembering the conceptual relationship. We would have to do the work remembering which value corresponds to which suit. This is...