I\'m currently reading about enums in C. I understand how they work, but can\'t figure out situations where they could be useful. Can you give me some simple examples where the
Sometime you want to express something that is finite and discrete. An example from the GNU C Programming tutorial are compass directions.
enum compass_direction
{
north,
east,
south,
west
};
Another example, where the ability of enums to correspond to integers comes in handy, could be status codes.
Usually you start the OK code with 0, so it can be used in if
constructs.
They're often used to group related values together:
enum errorcode {
EC_OK = 0,
EC_NOMEMORY,
EC_DISKSPACE,
EC_CONNECTIONBROKE,
EC_KEYBOARD,
EC_PBCK
};
Enums are primarily used because they are easier to read than integer numbers to programmers. For example, you can create an enum that represents dayy in a week.
enum DAY {Monday, Tuesday....}
Monday is equivalent to integer 0, Tuesday = Monday + 1 etc.
So instead of integers thta represent each week day, you can use a new type DAY to represent this same concept. It is easier to read for the programmer, but it means the same to the compiler.
Enums are just a way to declare constant values with more maintainability. The benefits include:
int monday = SUNDAY + 1
.Use them when you have a finite list of distinct, related values, as in the suits of a deck of cards. Avoid them when you have an effectively unbounded list or a list that could often change, as in the set of all car manufacturers.
The concept behind an enum is also sometimes called an "enumerated type". That is to say, it's a type all of whose possible values are named and listed as part of the definition of the type.
C actually diverts from that a bit, since if a
and b
are values in an enum, then a|b
is also a valid value of that type, regardless of whether it's listed and named or not. But you don't have to use that fact, you can use an enum just as an enumerated type.
They're appropriate whenever you want a variable whose possible values each represent one of a fixed list of things. They're also sometimes appropriate when you want to define a bunch of related compile-time constants, especially since in C (unlike C++), a const int
is not a compile-time constant.
I think that their most useful properties are:
1) You can concisely define a set of distinct constants where you don't really care what the values are as long as they're different. typedef enum { red, green, blue } color;
is better than:
#define red 0
#define green 1
#define blue 2
2) A programmer, on seeing a parameter / return value / variable of type color
, knows what values are legal and what they mean (or anyway knows how to look that up). Better than making it an int
but documenting "this parameter must be one of the color values defined in some header or other".
3) Your debugger, on seeing a variable of type color
, may be able to do a reverse lookup, and give red
as the value. Better than you looking that up yourself in the source.
4) The compiler, on seeing a switch
of an expression of type color
, might warn you if there's no default
and any of the enumerated values is missing from the list of case
s. That helps avoid errors when you're doing something different for each value of the enum.