assignment of integer to enumerated value

Discussion in 'C Programming' started by John Goche, Nov 24, 2006.

  1. John Goche

    John Goche Guest

    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
     
    John Goche, Nov 24, 2006
    #1
    1. Advertising

  2. 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
     
    Frederick Gotham, Nov 24, 2006
    #2
    1. Advertising

  3. John Goche

    Ian Collins Guest

    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.
     
    Ian Collins, Nov 24, 2006
    #3
  4. John Goche

    Eric Sosman Guest

    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
    lid
     
    Eric Sosman, Nov 24, 2006
    #4
  5. Ian Collins <> 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) <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.
     
    Keith Thompson, Nov 24, 2006
    #5
  6. 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
     
    Frederick Gotham, Nov 24, 2006
    #6
  7. John Goche

    Eric Sosman Guest

    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
    lid
     
    Eric Sosman, Nov 24, 2006
    #7
  8. John Goche

    Ian Collins Guest

    Keith Thompson wrote:
    > Ian Collins <> 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.
     
    Ian Collins, Nov 24, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cristian
    Replies:
    6
    Views:
    1,518
    David R Brooks
    Oct 13, 2004
  2. Marek Ponca

    Enumerated Type in assertion ?

    Marek Ponca, Jan 10, 2005, in forum: VHDL
    Replies:
    2
    Views:
    3,305
    Jonathan Bromley
    Jan 10, 2005
  3. rfractal30
    Replies:
    6
    Views:
    494
    rfractal30
    Feb 25, 2005
  4. Robben
    Replies:
    14
    Views:
    573
    Old Wolf
    Dec 27, 2005
  5. Colin Beighley

    Enumerated integer type

    Colin Beighley, Jun 24, 2011, in forum: VHDL
    Replies:
    2
    Views:
    962
    backhus
    Jun 27, 2011
Loading...

Share This Page