TRUE and FALSE
Rahul Dhesi
dhesi%cirrusl at oliveb.ATC.olivetti.com
Fri Aug 31 11:57:23 AEST 1990
In <1990Aug29.153917.28110 at warwick.ac.uk> asrap at warwick.ac.uk (Sean
Legassick) explains why he might want to use enum {false, true} for
booleans in C and continues:
>I would be interested in anyone who thinks an int declaration of
>a boolean variable is clearer.
It depends. Try this:
typedef enum {false, true} bool;
...
while ((c = getc(stdin)) != EOF) {
bool got_digit;
got_digit = isdigit(c);
...
if (got_digit == true) {
...
}
}
Since isdigit() is not guaranteed to return 0 or 1, the value of
got_digit could be something else (e.g. 4). Then, even when got_digit
is true by C conventions (nonzero), it isn't necessarily equal to its
enumeration value "true".
No matter what you do, you cannot in C get around this:
Although the result of a boolean condition is always 0 or 1, all
nonzero values are considered to be true in a boolean context.
All mechanisms that try to represent this fact using only two
values (e.g. TRUE and FALSE, or enum {false, true}) are likely to
lead to bad code that looks good.
--
Rahul Dhesi <dhesi%cirrusl at oliveb.ATC.olivetti.com>
UUCP: oliveb!cirrusl!dhesi
More information about the Comp.lang.c
mailing list