C programming style poll
Donald Wallace Rouse II
dwr at ccieng6.UUCP
Wed May 2 09:01:35 AEST 1984
xxxxxxxxxxxxxxxxxxxxxxxx
>
> (1) y = (x == 5);
>
>
> (2) y = 0;
> if (x == 5)
> y = 1;
>
> By the way, I agree that the first form above may not be
> guaranteed to be entirely equivalent to the others, but if y is
> assigned to a Boolean expression, it should be tested with
>
> if (y)
> if (!y)
> or possibly
> if (y == 0)
> if (y != 0)
> but never with
> if (y == 1)
> if (y != 1)
According to the K&R bible, p. 41 at the top:
Another useful form of type conversion is that
relational expressions like i > j and logical
expressions connected by && and || are defined to have
value 1 if true, and 0 if false. Thus the assignment
isdigit = c >= '0' && c <= '9';
sets isdigit to 1 if c is a digit and to 0 if not.
Therefore, it IS legal (though bad form, in my opinion)
to use the form "(y == 1)" or "(y != 1)".
I use this definition occasionally to access arrays, as in:
char * tf [] = {"false", "true"};
...
printf ("The supposition that x is 0 is %s\n", tf [x == 0]);
D2
More information about the Comp.lang.c
mailing list