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

Dick Dunn rcd at opus.UUCP
Wed Sep 18 16:46:31 AEST 1985


> We're going though the usual arguments about how the type 'boolean'
> should be fudged, and I came across this example:
> 
> 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
   ...Lately it occurs to me what a long, strange trip it's been.



More information about the Comp.lang.c mailing list