Enum autoincrement question

Discussion in 'C Programming' started by raphfrk@gmail.com, Oct 14, 2010.

  1. Guest

    Hi,

    Assuming I define an enum as:

    enum cardsuit {
    CLUBS = 0,
    DIAMONDS,
    HEARTS,
    SPADES
    };

    Can I be sure that the integer corresponding to DIAMOND is 1?

    If so what would happen with:

    enum cardsuit {
    CLUBS = 3,
    DIAMONDS = 2,
    HEARTS,
    SPADES
    };

    Would HEARTS end up as 4 and is that guaranteed?
    , Oct 14, 2010
    #1
    1. Advertising

  2. BartC Guest

    <> wrote in message
    news:...
    > Hi,
    >
    > Assuming I define an enum as:
    >
    > enum cardsuit {
    > CLUBS = 0,
    > DIAMONDS,
    > HEARTS,
    > SPADES
    > };
    >
    > Can I be sure that the integer corresponding to DIAMOND is 1?


    The first enum is always 0 anyway.

    > If so what would happen with:
    >
    > enum cardsuit {
    > CLUBS = 3,
    > DIAMONDS = 2,
    > HEARTS,
    > SPADES
    > };
    >
    > Would HEARTS end up as 4 and is that guaranteed?


    The values assigned auto-increment from whatever the previous one was. So
    HEARTS would be 3.

    If you override the value, then that just resets the current ordinal value
    and it will count on from there.

    But if you're not confident about this, then you can just override every
    value. This then allows you to re-order the names if you wish.

    --
    Bartc
    BartC, Oct 14, 2010
    #2
    1. Advertising

  3. "" <> writes:

    > Assuming I define an enum as:
    >
    > enum cardsuit {
    > CLUBS = 0,
    > DIAMONDS,
    > HEARTS,
    > SPADES
    > };
    >
    > Can I be sure that the integer corresponding to DIAMOND is 1?


    Yes.

    > If so what would happen with:
    >
    > enum cardsuit {
    > CLUBS = 3,
    > DIAMONDS = 2,
    > HEARTS,
    > SPADES
    > };
    >
    > Would HEARTS end up as 4 and is that guaranteed?


    No. The numbering is from the last explicitly numbered item so HEARTS
    is 3 and SPADES will be 4.

    --
    Ben.
    Ben Bacarisse, Oct 14, 2010
    #3
  4. John Bode Guest

    On Oct 14, 11:45 am, "" <> wrote:
    > Hi,
    >
    > Assuming I define an enum as:
    >
    > enum cardsuit {
    >    CLUBS = 0,
    >    DIAMONDS,
    >    HEARTS,
    >    SPADES
    >
    > };
    >
    > Can I be sure that the integer corresponding to DIAMOND is 1?
    >


    Yes.

    6.7.2.2 Enumeration Specifiers
    ....
    3 The identifiers in an enumerator list are declared as constants that
    have type int and may appear wherever such are permitted.109) An
    enumerator with = defines its enumeration constant as the value of the
    constant expression. If the first enumerator has no =, the value of
    its enumeration constant is 0. Each subsequent enumerator with no =
    defines its enumeration constant as the value of the constant
    expression obtained by adding 1 to the value of the previous
    enumeration constant. (The use of enumerators with = may produce
    enumeration constants with values that duplicate other values in the
    same enmeration.) The enumerators of an enumeration are also known as
    its members.

    > If so what would happen with:
    >
    > enum cardsuit {
    >    CLUBS = 3,
    >    DIAMONDS = 2,
    >    HEARTS,
    >    SPADES
    >
    > };
    >
    > Would HEARTS end up as 4 and is that guaranteed?


    HEARTS would end up with 3.
    John Bode, Oct 14, 2010
    #4
  5. Eric Sosman Guest

    On 10/14/2010 12:45 PM, wrote:
    > Hi,
    >
    > Assuming I define an enum as:
    >
    > enum cardsuit {
    > CLUBS = 0,
    > DIAMONDS,
    > HEARTS,
    > SPADES
    > };
    >
    > Can I be sure that the integer corresponding to DIAMOND is 1?


    No, but the value of DIAMONDS (with an S) is guaranteed to be 1.
    Also, HEARTS=2 and SPADES=3.

    > If so what would happen with:
    >
    > enum cardsuit {
    > CLUBS = 3,
    > DIAMONDS = 2,
    > HEARTS,
    > SPADES
    > };
    >
    > Would HEARTS end up as 4 and is that guaranteed?


    No: You would have CLUBS=3, DIAMONDS=2, HEARTS=3 (one greater
    than DIAMONDS), and SPADES=4 (one greater than HEARTS).

    --
    Eric Sosman
    lid
    Eric Sosman, Oct 15, 2010
    #5
  6. Guest

    On Oct 15, 3:07 am, Eric Sosman <> wrote:
    >      No: You would have CLUBS=3, DIAMONDS=2, HEARTS=3 (one greater
    > than DIAMONDS), and SPADES=4 (one greater than HEARTS).


    Thanks for the info.
    , Oct 15, 2010
    #6
    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. Munnki
    Replies:
    2
    Views:
    525
    Munnki
    Dec 18, 2003
  2. Gene Gorokhovsky
    Replies:
    0
    Views:
    456
    Gene Gorokhovsky
    Jul 17, 2003
  3. RA
    Replies:
    0
    Views:
    338
  4. RA
    Replies:
    1
    Views:
    421
    Darrin J Olson
    Oct 22, 2003
  5. fatboycanteen

    About Rowstate and AutoIncrement

    fatboycanteen, Dec 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    314
    fatboycanteen
    Dec 12, 2003
Loading...

Share This Page