Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > assignment of integer to enumerated value

Reply
Thread Tools

assignment of integer to enumerated value

 
 
John Goche
Guest
Posts: n/a
 
      11-24-2006
Hello,

The following program compiler and runs fine under gcc and produces
the output 3. However, I am not sure whether such behavior is legal.
In particular, as a related question, how would I go about checking
that a supplied integer is contained within the set of values of an
enumeration? Is there a way to do this?

Thanks,

JG

#include <stdio.h>

int main() {
enum { A = 1, B = 2 } foo;
foo = 3;
printf("%d\n", foo);
}

output: 3

 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      11-24-2006
John Goche:

> #include <stdio.h>
>
> int main() {
> enum { A = 1, B = 2 } foo;
> foo = 3;
> printf("%d\n", foo);
> }
>
> output: 3



I'm not quite sure how accurate it is, but Bjarne Stroustrup gave an
explanation of this before. In order to determine the highest value you can
store in an enum, you:

(1) Take the largest number which exists in the definition of the enum
(i.e. B = 2).
(2) Count the amount of bits needed to represent that number.
(3) Sets all the bits to yield the max value.

This would imply that the max value for your enum is 3 (i.e. 11 in binary).

--

Frederick Gotham
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-24-2006
John Goche wrote:
> Hello,
>
> The following program compiler and runs fine under gcc and produces
> the output 3. However, I am not sure whether such behavior is legal.


Enums are broken (not proper types) in C, so it is legal to assign a int
to an enum.

--
Ian Collins.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-24-2006
Frederick Gotham wrote:
> John Goche:
>
>
>>#include <stdio.h>
>>
>>int main() {
>> enum { A = 1, B = 2 } foo;
>> foo = 3;
>> printf("%d\n", foo);
>>}
>>
>>output: 3

>
>
>
> I'm not quite sure how accurate it is, but Bjarne Stroustrup gave an
> explanation of this before. In order to determine the highest value you can
> store in an enum, you:
>
> (1) Take the largest number which exists in the definition of the enum
> (i.e. B = 2).
> (2) Count the amount of bits needed to represent that number.
> (3) Sets all the bits to yield the max value.


The recipe doesn't work for (e.g.)

enum { LOW = -100, HIGH = 1 } x;

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-24-2006
Ian Collins <(E-Mail Removed)> writes:
> John Goche wrote:
>> The following program compiler and runs fine under gcc and produces
>> the output 3. However, I am not sure whether such behavior is legal.

>
> Enums are broken (not proper types) in C, so it is legal to assign a int
> to an enum.


Well, that depends on what you mean by "broken" and/or "proper".

An enumerated type is a distinct type, but there's an implicit
conversion between any enum type and any integer type, just as there
is between any two integer types, or between an integer type and a
float type. An enumeration *constant* is of type int, not of the
enumerated type.

C99 6.7.2.2 says:

Each enumerated type shall be compatible with char, a signed
integer type, or an unsigned integer type. The choice of type is
implementation-defined, but shall be capable of representing the
values of all the members of the enumeration.

Since the constants are of type int, there probably wouldn't be much
point in making an enumerated type bigger than int, but that's up to
the compiler.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      11-24-2006
Ian Collins:

>> The following program compiler and runs fine under gcc and produces
>> the output 3. However, I am not sure whether such behavior is legal.

>
> Enums are broken (not proper types) in C, so it is legal to assign a int
> to an enum.



Ah yes, I must have missed that part of the Standard.

--

Frederick Gotham
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-24-2006
John Goche wrote:

> Hello,
>
> The following program compiler and runs fine under gcc and produces
> the output 3. However, I am not sure whether such behavior is legal.


It's legal, unfortunately. C's enum types are not really
"enumerations" of the kind some other languages offer, but just
integers. The compiler gets to choose any flavor of integer it
wants (unsigned short, signed char, ...), so long as the chosen
flavor can handle all the enum's named values. Thereafter, the
enum variable just acts like an integer variable of the chosen
type, and any value that would be legal for a plain integer of
that type is also legal for the enum variable.

Occasionally this freedom can be exploited usefully, as in

enum font_flags = { DEFAULT = 0,
BOLD = 1, ITALIC = 2, UNDERLINED = 4,
REVERSEVIDEO = 8, BLINKING = 16 };
...
enum font_flags style = DEFAULT;
if (important_message)
style |= BOLD;
if (annoy_the_viewer)
style |= BLINKING;
display_string ("Hello, world!", style);

> In particular, as a related question, how would I go about checking
> that a supplied integer is contained within the set of values of an
> enumeration? Is there a way to do this?


C itself offers no way to do this. Some compilers in some
situations may issue warnings for legal but "suspicious" uses of
enums, but the language is of little help. The language also offers
no answer to "What are the largest and smallest declared values for
some enum type?", nor even "What are the largest and smallest possible
values for some enum type?" -- and if you want to do "Iterate over
all the declared values of this enum type," you're out of luck.

C's enums have little "power" and few worthwhile uses.

> Thanks,
>
> JG
>
> #include <stdio.h>
>
> int main() {
> enum { A = 1, B = 2 } foo;
> foo = 3;
> printf("%d\n", foo);
> }
>
> output: 3


--
Eric Sosman
(E-Mail Removed)lid

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-24-2006
Keith Thompson wrote:
> Ian Collins <(E-Mail Removed)> writes:
>
>>John Goche wrote:
>>
>>>The following program compiler and runs fine under gcc and produces
>>>the output 3. However, I am not sure whether such behavior is legal.

>>
>>Enums are broken (not proper types) in C, so it is legal to assign a int
>>to an enum.

>
>
> Well, that depends on what you mean by "broken" and/or "proper".
>
> An enumerated type is a distinct type, but there's an implicit
> conversion between any enum type and any integer type, just as there
> is between any two integer types, or between an integer type and a
> float type. An enumeration *constant* is of type int, not of the
> enumerated type.
>

We've had the argument before, but it's the implicit conversion from int
to enumerated type I consider broken. The set of values for the type is
there for a reason, being able to assign another value to an object of
that type just strikes me as wrong.

I prefer the C++ rules for enumerated types, mainly for the compile time
type checking when an enum is used as a function parameter.

--
Ian Collins.
 
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
Enumerated integer type Colin Beighley VHDL 2 06-27-2011 05:59 AM
integer int i; *i++ and ++*i have a different integer value after the increment Robben C++ 14 12-27-2005 03:43 AM
strange recursivion with enumerated types - how is this possible? rfractal30 Java 6 02-25-2005 02:38 AM
Enumerated Type in assertion ? Marek Ponca VHDL 2 01-10-2005 10:32 AM
Reading enumerated state variables cristian VHDL 6 10-13-2004 10:50 PM



Advertisments