Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Incrementing an enum variable?

Reply
Thread Tools

Incrementing an enum variable?

 
 
Peter Tipton
Guest
Posts: n/a
 
      06-07-2010
/* 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.

--
* *
*** Peter ***
* *
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-07-2010
On 6/7/2010 5:11 PM, Peter Tipton wrote:
> /* 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.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      06-07-2010
Peter Tipton <(E-Mail Removed)> writes:

> 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.
--
Ben Pfaff
http://benpfaff.org
 
Reply With Quote
 
bart.c
Guest
Posts: n/a
 
      06-07-2010

"Peter Tipton" <(E-Mail Removed)> wrote in message
news:hujn9q$idc$(E-Mail Removed)...
> /* 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.

--
Bartc

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ENUM server for private ENUM kael UK VOIP 2 02-25-2007 11:54 AM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
enum within an enum - Java 6 06-13-2005 12:51 AM
Including an enum within another enum, possible? mrhicks C Programming 2 06-10-2004 03:00 AM
How to enum an enum? Ernst Murnleitner C++ 5 11-13-2003 11:06 AM



Advertisments