Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Enum autoincrement question

Reply
Thread Tools

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,
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?
 
Reply With Quote
 
 
 
 
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,
> 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

 
Reply With Quote
 
 
 
 
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,
> 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.
 
Reply With Quote
 
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,
> * *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.
 
Reply With Quote
 
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,
> 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
(E-Mail Removed)lid
 
Reply With Quote
 
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: an enum question - how to convert enum type to a string? Roedy Green Java 0 02-27-2010 04:52 AM
Re: an enum question - how to convert enum type to a string? Wojtek Java 1 02-27-2010 03:44 AM
Re: an enum question - how to convert enum type to a string? Lew Java 0 02-27-2010 03:40 AM
Re: an enum question - how to convert enum type to a string? Eric Sosman Java 3 02-26-2010 03:36 PM
Re: ViewState + AutoIncrement -- Okay; Session + AutoIncrement -- Not okay Gene Gorokhovsky ASP .Net 0 07-17-2003 02:48 PM



Advertisments