Incrementing an enum variable?

Discussion in 'C Programming' started by Peter Tipton, Jun 7, 2010.

  1. Peter Tipton

    Peter Tipton Guest

    /* 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 ***
    * *
     
    Peter Tipton, Jun 7, 2010
    #1
    1. Advertising

  2. Peter Tipton

    Eric Sosman Guest

    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
    lid
     
    Eric Sosman, Jun 7, 2010
    #2
    1. Advertising

  3. Peter Tipton

    Ben Pfaff Guest

    Peter Tipton <> 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
     
    Ben Pfaff, Jun 7, 2010
    #3
  4. Peter Tipton

    bart.c Guest

    "Peter Tipton" <> wrote in message
    news:hujn9q$idc$...
    > /* 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
     
    bart.c, Jun 7, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daz
    Replies:
    5
    Views:
    409
    Default User
    May 1, 2006
  2. Gary

    problem incrementing my variable

    Gary, Jan 27, 2009, in forum: C Programming
    Replies:
    43
    Views:
    970
  3. Matt Brooks
    Replies:
    9
    Views:
    187
    Matt Brooks
    Sep 21, 2009
  4. mr_burns
    Replies:
    1
    Views:
    125
  5. Jonas Yorg
    Replies:
    11
    Views:
    165
    Jonas Yorg
    Sep 2, 2003
Loading...

Share This Page