Velocity Reviews > Enum autoincrement question

# Enum autoincrement question

raphfrk@gmail.com
Guest
Posts: n/a

 10-14-2010
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?

BartC
Guest
Posts: n/a

 10-14-2010

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

value. This then allows you to re-order the names if you wish.

--
Bartc

Ben Bacarisse
Guest
Posts: n/a

 10-14-2010
"(E-Mail Removed)" <(E-Mail Removed)> 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.

John Bode
Guest
Posts: n/a

 10-14-2010
On Oct 14, 11:45*am, "(E-Mail Removed)" <(E-Mail Removed)> 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.

Eric Sosman
Guest
Posts: n/a

 10-15-2010
On 10/14/2010 12:45 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

> 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
(E-Mail Removed)lid

raphfrk@gmail.com
Guest
Posts: n/a

 10-15-2010
On Oct 15, 3:07*am, Eric Sosman <(E-Mail Removed)> 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.