Enumerations

Discussion in 'C Programming' started by Christian Christmann, May 17, 2006.

  1. Hi,

    is it valid to assign an enumeration the enumeration
    value of another enumeration?

    Example:

    enum type1 { A = -9, B, C, D, E };
    enum enum_type1 enum1 = A;
    enum type2 { F = 10, G = 20, H = 30, I = 40, J = 50 };
    int main( void )
    {
    enum type2 enum2 = A; //OK???

    return 0;
    }

    Thank you.
    Chris
     
    Christian Christmann, May 17, 2006
    #1
    1. Advertising

  2. Christian Christmann

    Guest

    , May 17, 2006
    #2
    1. Advertising

  3. Christian Christmann

    Eric Sosman Guest

    Christian Christmann wrote On 05/17/06 11:08,:
    > Hi,
    >
    > is it valid to assign an enumeration the enumeration
    > value of another enumeration?
    >
    > Example:
    >
    > enum type1 { A = -9, B, C, D, E };
    > enum enum_type1 enum1 = A;


    The `enum_' part doesn't belong.

    > enum type2 { F = 10, G = 20, H = 30, I = 40, J = 50 };
    > int main( void )
    > {
    > enum type2 enum2 = A; //OK???
    >
    > return 0;
    > }


    Yes. enum types are not "rigorous" in this sense.
    The enum type itself is just some kind of an integer
    (the compiler gets to choose), and the enumerated
    values are just integer constants. There's no further
    enforcement of type.

    That said, some compilers may issue warnings for
    some kinds of abuse of enum types. If they do, it's
    just a courtesy, though, in the same way that some
    compilers will issue a warning for `if (a = b)' --
    the construct is valid, but the context is suspect.

    --
     
    Eric Sosman, May 17, 2006
    #3
  4. Christian Christmann

    Vladimir Oka Guest

    Christian Christmann wrote:
    > Hi,
    >
    > is it valid to assign an enumeration the enumeration
    > value of another enumeration?


    Compiler certainly won't stop you, so I guess I'd say it's not invalid.
    IMHO, C is broken in this respect.

    However, I'd be worried about your design if you're planning to use
    this "feature".

    > Example:
    >
    > enum type1 { A = -9, B, C, D, E };
    > enum enum_type1 enum1 = A;


    ITYM:

    enum type1 enum1 = A;

    Speaks volumes for pasting rather than typing the code in a post.

    > enum type2 { F = 10, G = 20, H = 30, I = 40, J = 50 };
    > int main( void )
    > {
    > enum type2 enum2 = A; //OK???


    Using C++ style comments (although valid in C99) is a bad idea in
    posts.

    > return 0;
    > }
     
    Vladimir Oka, May 17, 2006
    #4
  5. Christian Christmann

    Vladimir Oka Guest

    wrote:
    > yep, you can do this,


    Do what? Quote what you're replying to. Read
    <http://cfaj.freeshell.org/google/>.

    > in fact, you can just view the A as a integer (
    > -9), you can get the enum's discritption at
    > http://msdn2.microsoft.com/en-US/library/whbyts4t.aspx or so, have fun
     
    Vladimir Oka, May 17, 2006
    #5
  6. Christian Christmann

    Default User Guest

    Eric Sosman wrote:

    >
    >
    > Christian Christmann wrote On 05/17/06 11:08,:
    > > Hi,
    > >
    > > is it valid to assign an enumeration the enumeration
    > > value of another enumeration?


    > Yes. enum types are not "rigorous" in this sense.
    > The enum type itself is just some kind of an integer
    > (the compiler gets to choose), and the enumerated
    > values are just integer constants. There's no further
    > enforcement of type.


    I suppose that it could be unsafe in some circumstances, if one enum
    had a smaller range than the other. You could get overflow.



    Brian
     
    Default User, May 17, 2006
    #6
  7. > Eric Sosman wrote:

    >> The enum type itself is just some kind of an integer
    >> (the compiler gets to choose), and the enumerated
    >> values are just integer constants. There's no further
    >> enforcement of type.

    >
    > I suppose that it could be unsafe in some circumstances, if one enum
    > had a smaller range than the other. You could get overflow.


    How can this happen? Does not every enum has the same range namely
    signed integer?

    Paul
     
    Christian Christmann, May 18, 2006
    #7
  8. Christian Christmann

    Vladimir Oka Guest

    Christian Christmann wrote:
    > > Eric Sosman wrote:

    >
    > >> The enum type itself is just some kind of an integer
    > >> (the compiler gets to choose), and the enumerated
    > >> values are just integer constants. There's no further
    > >> enforcement of type.

    > >
    > > I suppose that it could be unsafe in some circumstances, if one enum
    > > had a smaller range than the other. You could get overflow.

    >
    > How can this happen? Does not every enum has the same range namely
    > signed integer?


    Not necessarily:

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

    Enumeraiton constants, though:

    6.4.4.3p2
    An identifier declared as an enumeration constant has type int.
     
    Vladimir Oka, May 18, 2006
    #8
  9. Christian Christmann

    Eric Sosman Guest

    Christian Christmann wrote:
    >>Eric Sosman wrote:

    >
    >
    >>>The enum type itself is just some kind of an integer
    >>>(the compiler gets to choose), and the enumerated
    >>>values are just integer constants. There's no further
    >>>enforcement of type.

    >>


    [Attribution lost; the next paragraph is by Default User]

    >>I suppose that it could be unsafe in some circumstances, if one enum
    >>had a smaller range than the other. You could get overflow.

    >
    >
    > How can this happen? Does not every enum has the same range namely
    > signed integer?


    No. The compiler chooses the type underlying each enum
    type, and it may choose differently for different enums. It
    must choose a type that is some kind of integer, and it must
    choose a type that can represent all the enumerated values
    for the enum, but there are no other restrictions.

    For example, given

    enum small { SMALL_A = -1, SMALL_B = 1 };
    enum large { LARGE_A = 0, LARGE_B = 1 };

    the compiler might choose `signed char' and `unsigned char'
    as the two underlying types. (The type for `enum_small' must
    be signed because SMALL_A is negative, but `enum_large' has
    no negative enumerated values so an unsigned type is possible.)
    Now if the programmer does

    enum large x = -1; /* stores UCHAR_MAX */
    enum small y = x; /* too large for `signed char' */

    .... he runs the risk that Default User mentions. However, there
    is still no "enforcement," in the sense that the compiler is not
    required to issue a diagnostic for the potential bad behavior.

    --
    Eric Sosman
    lid
     
    Eric Sosman, May 18, 2006
    #9
    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. clintonG

    HttpStatusCode Enumerations...

    clintonG, May 5, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    3,953
    clintonG
    May 6, 2004
  2. exquisitus
    Replies:
    12
    Views:
    22,436
    Tilman Bohn
    Feb 19, 2005
  3. exquisitus
    Replies:
    2
    Views:
    452
    Tor Iver Wilhelmsen
    Apr 24, 2005
  4. Chanchal
    Replies:
    4
    Views:
    404
    Thomas Hawtin
    Nov 29, 2005
  5. Joyce
    Replies:
    1
    Views:
    2,087
    Stan Kitsis [MSFT]
    Feb 18, 2005
Loading...

Share This Page