value of TRUE???

Doug Gwyn gwyn at smoke.BRL.MIL
Wed Mar 8 12:20:36 AEST 1989


In article <987 at infmx.UUCP> kevinf at infmx.UUCP (Kevin Franden) writes:
>given: 	:The if() statement will evaluate to true provided that
>	 the argument does not evaluate to 0.  (ie a=3; if (a)...)
>Is then the value of true any nonzero integer?
>If it's not, is it equivelent to a nonzero integer?
>What does if (a=3) evaluate to?

You're adding to your own confusion by trying to bundle too much
together.

The C language itself has no official "true" value, so trying to
decide what that value "is" is inherently misleading.

First of all, the "if" STATEMENT does not have a value.  You cannot
say	a = if(x)y;else z;
(Some Algol-like languages would support this, but C doesn't; it has
a ?: operator instead.)

Next, the value of what would appear to be a Boolean or relational
expression is either 0 or 1, and it's an arithmetic expression in C.

Third, the condition used for if(), while(), etc. is any arithmetic
expression, not just those that appear to be Boolean.  The condition
is taken to be "true" if it has any nonzero value.

Thus,	if(a=3)	assigns the value 3 to the variable a, and then,
since the assignment expression has the value 3 which is nonzero,
the "true" branch of the "if" statement is taken.

The assignment statement
	i = j > 2;
will give the variable i the value 0 unless the value of j exceeds
2, in which case i will be assigned the value 1.

I don't recommend mixing conceptually arithmetic and Boolean operations
even though the C language is designed to allow it.



More information about the Comp.lang.c mailing list