if (X) <==> if (X != 0) (enums)

Chris Crampton cmc at rlvd.UUCP
Thu Sep 26 04:43:57 AEST 1985


In article <57 at opus.UUCP> rcd at opus.UUCP (Dick Dunn) writes:
>> typedef enum {FALSE, TRUE} bool;
>> ...
>>     if (flag) {
>> ...
>>     if (flag != 0) {
>> ...
>> The BSD 4.2 C compiler is happy about the first 'if', but warns about
>> 'enumeration type clash' on the second. This at least is one case where
>> 'if (X)' isn't the same as 'if (X != 0)'.
>
>I remember Dennis Ritchie commenting about enums in C; the general idea was
>that they were "ripp'd untimely from the womb" as it were, so there's woom
>er, room, for improvement.  Some of the behavior of enums is for the dogs;
>it's high time someone said, "Out, damn Spot!", got us off our MacDuffs, and
>cleaned them up somehow.  I discovered, courtesy one of our local errant
>programmers, that given a type:
>	typedef enum {T0, T1, T2} ternary;
>and a declaration:
>	ternary t;
>although it is not permitted, with the 4.x BSD compilers, to say:
>	if (t!=0) ...
>it IS permitted to say
>	if (!t)
>My reaction to the use of ! on a ternary value is, quid pro quo, !
>If enums are ints, both ifs should be allowed; if they are distinct animals
>(which I might hope had been the int-ention), neither should be allowed.
>-- 
>Dick Dunn	{hao,ucbvax,allegra}!nbires!rcd		(303)444-5710 x3086

At the recent EUUG conference, Mike Banahan of The Instuction Set gave
a summary of ANSI C. One thing I remember well was the statement "enum IS
int". This should hopefully clear things up in the future when we all have
our ANSI C compilers, but until then we will have to put up with the
various ways compilers handle enums. The compilers on the machines we
run here (Perq PNX, 4.2 ...) all give the previously described
"type clash" warnings and do not allow enums to index an array. But, I am
assured that there are many compilers which treat enums as ints (already)
and that there are indeed good reasons for doing so (run time checking of
proper enumerated type would be too expensive...etc - there were apparently
moves on the ANSI commitee to scrap enums altogether)

I am of the inclination that enums should be handled pretty much as they
are on our machines in that mixing enums and ints should only be allowed with an
appropriate cast, as this indicates that the programmer is prepared to take
the risk or has checked the value herself.

-- 
Chris M Crampton,	..!mcvax!ukc!rlvd!cmc
			Rutherford Appleton Labs, Didcot, OXON, U.K.
			+44 235 21900   ext. 6756
		



More information about the Comp.lang.c mailing list