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,
};

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,
};

Would HEARTS end up as 4 and is that guaranteed?

, Oct 14, 2010

2. BartCGuest

<> wrote in message
news:...
> Hi,
>
> Assuming I define an enum as:
>
> enum cardsuit {
> CLUBS = 0,
> DIAMONDS,
> HEARTS,
> };
>
> 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,
> };
>
> 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

3. Ben BacarisseGuest

"" <> writes:

> Assuming I define an enum as:
>
> enum cardsuit {
> CLUBS = 0,
> DIAMONDS,
> HEARTS,
> };
>
> 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,
> };
>
> 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
4. John BodeGuest

On Oct 14, 11:45 am, "" <> wrote:
> Hi,
>
> Assuming I define an enum as:
>
> enum cardsuit {
>    CLUBS = 0,
>    DIAMONDS,
>    HEARTS,
>
> };
>
> 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,
>
> };
>
> Would HEARTS end up as 4 and is that guaranteed?

HEARTS would end up with 3.

John Bode, Oct 14, 2010
5. Eric SosmanGuest

On 10/14/2010 12:45 PM, wrote:
> Hi,
>
> Assuming I define an enum as:
>
> enum cardsuit {
> CLUBS = 0,
> DIAMONDS,
> HEARTS,
> };
>
> 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,
> };
>
> 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
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