Re: Why no/limited checking on enum values ?
From: Chris Torek (nospam_at_torek.net)
Date: 01/17/05
- Next message: Lawrence Kirby: "Re: c equivilant to "copy""
- Previous message: Thomas Matthews: "Re: coloring a pixel"
- In reply to: xarax: "Re: Why no/limited checking on enum values ?"
- Next in thread: Keith Thompson: "Re: Why no/limited checking on enum values ?"
- Reply: Keith Thompson: "Re: Why no/limited checking on enum values ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 17 Jan 2005 17:18:18 GMT
>"Nebula" <noname@example.com> wrote in message
>news:41ebca38@news.wineasy.se...
>> Consider
>> enum Side {Back,Front,Top,Bottom};
>> enum Side a;
>> Now, why is
>> a = 124;
>> legal (well it really is an integer, but still,
>> checking could be performed..) ?
>> Wouldn't enums be more useful if there was a
>> bit more typechecking ?
(Some compilers actually do some sort(s) of extra checking. No
diagnostics are required here, but compilers are always allowed to
produce as many warning messages as the compiler-writer wants.
Whether or not those extra warning are good or desirable is another
question entirely....)
In article <W3SGd.8999$pZ4.748@newsread1.news.pas.earthlink.net>
xarax <xarax@email.com> wrote:
>An enum type is a symbolic name for an int. Declaring
Well, not quite. The enumeration *members* are names for "int"s,
but the enumerated *type* -- here "enum Side" -- is not necessarily
an int. Some compilers either always, or optionally, shorten the
type to the smallest one that will hold its members. (With gcc,
use "-fshort-enum" to turn this on.)
>enum Side a;
>
>is simply declaring 'a' to be an int.
Or perhaps a "char" or "unsigned char":
enum A { Zero, One_twenty_seven = 127 }; /* always fits in char */
enum B { Two_fifty_five = 255 }; /* always fits in unsigned char */
enum C { Three_two_seven_six_seven = 32767 }; /* always fits in int */
I have not studied the C99 text closely yet, but my reading of the
C89 standard appears to me to allow even "enum C" to use an unsigned
char, despite the fact that one should be able to do:
enum C x = Three_two_seven_six_seven;
and on real implementations, that would make x have the value 127.
(I would hope that any compiler that did this, even if the C
standards do allow it, would not survive in the marketplace....)
Note that:
enum D { Large = 2147483647 };
is OK if and only if INT_MAX is at least 2147483647, e.g., typical
32-bit systems today. In other words, you can use this in a 32-bit
compiler but not in a 16-bit compiler. (I believe some compilers
have extensions to allow "enum"s to contain long and/or long long
members, but this *is* an extension; the C standards -- C89 and
C99 both -- do not require it.)
-- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603 email: forget about it http://web.torek.net/torek/index.html Reading email is like searching for food in the garbage, thanks to spammers.
- Next message: Lawrence Kirby: "Re: c equivilant to "copy""
- Previous message: Thomas Matthews: "Re: coloring a pixel"
- In reply to: xarax: "Re: Why no/limited checking on enum values ?"
- Next in thread: Keith Thompson: "Re: Why no/limited checking on enum values ?"
- Reply: Keith Thompson: "Re: Why no/limited checking on enum values ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|