Which do you prefer?
Code:
switch ( month ) {
case 9: case 4: case 6: case 11:
numdays = 30;
break;
}
or this
Code:
switch ( month ) {
case september: case april: case june: case november:
numdays = 30;
break;
}
Enums add to the readability of the code.
> enum just gives me +1 from the predecessor I picked.
> why not use a fixed number?
Not always, you can have
red = 1, amber = 2, green = 4
So why not have
Code:
#define red 1
#define amber 2
#define green 4
Enums have a measure of type safety, and would be understood by the debugger.
So for example, consider
Code:
enum { red = 1, amber = 2, green = 4 } traffic_t;
enum { apple, orange, banana } fruit_t;
traffic_t light = orange; // it's a fruit, not a colour
would draw some comment from the compiler.
If you just had int, and a bunch of #defines, all the compiler would see is
int light = 2;
In a debugger, you're likely to see light = red (meaningful), rather than light = 0 (much harder work).