Incrementing an enum variable?

P

Peter Tipton

/* Let's say I have an enum: */
enum Leds {
FIRST_LED = 0,
LED_RED = FIRST_LED,
LED_YLW,
LED_GRN,
NUM_LEDS

};

/* And a driver function (elsewhere) */
void turnOnLeds(enum Leds led);

/* And a function that wants to turn all the LED's on: */
void cycleLeds(void)
{
enum Leds led;

for (led = FIRST_LED; led < NUM_LEDS; ++led) /* <-- Line in question
*/
{
turnOnLed(led);
}

}

Now, on the line in question, should ++led be legal? I can see how it
might
not be but I'm sure I've done this before without compiler errors being
generated. So should I be able to increment an enum variable or not?

Thanks.
 
E

Eric Sosman

/* Let's say I have an enum: */
enum Leds {
FIRST_LED = 0,
LED_RED = FIRST_LED,
LED_YLW,
LED_GRN,
NUM_LEDS

};

/* And a driver function (elsewhere) */
void turnOnLeds(enum Leds led);

/* And a function that wants to turn all the LED's on: */
void cycleLeds(void)
{
enum Leds led;

for (led = FIRST_LED; led< NUM_LEDS; ++led) /*<-- Line in question
*/
{
turnOnLed(led);
}

}

Now, on the line in question, should ++led be legal? I can see how it
might
not be but I'm sure I've done this before without compiler errors being
generated. So should I be able to increment an enum variable or not?

Yes. An enum variable is some kind of an integer -- int,
unsigned char, whatever the compiler chooses so long as its range
includes all values of the enumerated constants. So you can
increment it, divide by it, right-shift it, and so on. You can
even (and this surprises people familiar with the enumerated types
in other languages) set it to a value unequal to all its enumerated
constants.

Note that the arithmetic is done only in terms of the numeric
value of the variable. In your example, the two enumerated
constants FIRST_LED and LED_RED are both zero, and the `for' loop
will run through the values 0,1,2 and stop before 3. It will *not*
run through FIRST_LED,LED_RED,LED_YLW,LED_GRN and stop before
NUM_LEDS; the fact that there are two names for zero doesn't mean
you'll visit zero twice.
 
B

Ben Pfaff

Peter Tipton said:
So should I be able to increment an enum variable or not?

Yes, you should, because there is nothing in the standard that
forbids it. Compilers might want about it, because there is
nothing in the standard that forbids that either.
 
B

bart.c

Peter Tipton said:
/* Let's say I have an enum: */
enum Leds {
FIRST_LED = 0,
LED_RED = FIRST_LED,
LED_YLW,
LED_GRN,
NUM_LEDS

};

/* And a driver function (elsewhere) */
void turnOnLeds(enum Leds led);

/* And a function that wants to turn all the LED's on: */
void cycleLeds(void)
{
enum Leds led;

for (led = FIRST_LED; led < NUM_LEDS; ++led) /* <-- Line in question
*/
{
turnOnLed(led);
}

}

Now, on the line in question, should ++led be legal? I can see how it
might
not be but I'm sure I've done this before without compiler errors being
generated. So should I be able to increment an enum variable or not?

Provided you don't expect to be able to increment to the next enum value,
where these are not consecutive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top