true and false in C (was Fun C typos (was *p++ = *q++))

Chris Torek chris at mimsy.UUCP
Sat Feb 18 15:33:20 AEST 1989


[sigh... all in the name of correctness]

Names no longer matter; someone wrote:
>>>... TRUE is not 0 in our implimentation.

so in article <15908 at mimsy.UUCP> I added:
>>... the true-value of any comparison operator in *any* correct C
>>compiler is 1 ....

In article <EJP.89Feb16173101 at faron.icd.abnet.COM> ejp at faron.icd.abnet.COM
(Ed Prochak) writes:
>Chris,
>	He said nothing about the value that resulted from the comparison.

Sure enough---but consider the original phrasing: `TRUE is not 0 *in
our implementation*'.  This implies that true (to be distinguished from
his TRUE, which was probably a macro defined as 1) has different values
in different implementations.  It does not.  C comparison operators
(which are the ones that produce true/false values) produce 0 and 1.
Always.

>He said, "TRUE is not 0". and that is correct.

Indeed.  But `3 < 5' is 1, not some implementation-defined value.

>A TRUE value in C does not have to be 1 (one) for it to be a
>"correct C compiler".

A truth value produced by an ostensibly boolean operator DOES have to
be 1 (one) for it to be correct.  Read on:

>In C
>	FALSE	=	0
>	TRUE	=	NOT FALSE

No, for C defines neither `TRUE' nor `FALSE'.  What it *does* define
is both the domain and the range for ostensibly-boolean tests.  Anything
that *accepts* an ostensibly-boolean value accepts *all* nonzero integer
values as `true' and only the integer value 0 as `false', but anything
that *produces* such a value produces either 0 or 1.  The *type* of
a `boolean' operator is `int'; the *range* of such an operator is
the set {0, 1}.

Specifically:

	if (expr) ...

accepts any non-zero value for `expr'.

	!(expr)

converts any non-zero expr to 0, and a zero-valued expr to 1.

	expr1 comparison-op expr2

performs the comparison, and if true, produces 1, otherwise produces
0.  Since if, while, do/while, and for conditions allow pointer-valued
expressions, the simplest way to think about them is to imagine that
all such statements are converted from

	if (expr)

to

	if ((expr) != 0)

during compilation.  The result is then tested against 0 and 1 and no
other values (none other being possible).  Thus only the logical
negation operator `!' need consider the possibility of values not in
the set {0, 1}.

(Obviously some computation can be elided by the compiler wherever
the value itself is not used---typically all `boolean' tests discard
the precise value, caring only about zero/nonzero---which is probably
what leads to the question over the `correct' value for a macro for
TRUE.  FALSE is easy; only 0 qualifies.  Any nonzero integer tests
true, but only one such integer---namely 1---is ever produced, so
while there is no `correct' answer, 1 is probably best.)

There, sufficient now?  :-)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list