There are times when we want a program or function variable to take only a limited number of values. For convenience, and to make the purpose of each value clear, each value in the set of possible values is given a name. We can think of this set as a grouping of 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 which value corresponds to which suit. This is an error-prone way of solving this problem.
We could, however, improve that solution by...