Enum autoincrement question

R

raphfrk

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?
 
B

BartC

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.
 
B

Ben Bacarisse

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.
 
J

John Bode

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.
 
E

Eric Sosman

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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top