value of TRUE???

Chris Torek chris at mimsy.UUCP
Tue Mar 7 14:30:19 AEST 1989


In article <987 at infmx.UUCP> kevinf at infmx.UUCP (Kevin Franden) writes:
>Hi,  I hope someone out there in netland can decide a bet
>I have with a colleague.  I am kinda new to this newsgroup
>and know that there are alot of C gurus (guri?) out there that
>can handle this.

A good C tutorial can also handle it, and is likely to provide you
with a single correct answer, rather than several conflicting ones.
It would take more work on your part to locate and read one; but that
would have the side effect of giving you quite a bit more information
(although I would like to think that my presentation is generally
more accurate and/or entertaining than theirs :-) ).

Anyway:

>given:	:The if() statement will evaluate to true provided that
>	 the argument does not evaluate to 0.  (ie a=3; if (a)...)

The *statement* does not produce a value.  The *expression* used as
an argument to `if' will produce the value <int,3> (assuming `a' is
an int), and control will move to the `true' section of the `if'.

>Is then the value of true any nonzero integer?
>If it's not, is it equivelent to a nonzero integer?

These questions are ill-formed.

C's flow-control statements (if, while, do/while, and for) require as
their conditional expression any expression returning an arithmetic or
pointer type.  The value produced by that expression is then compared
against zero (the zero having been converted to the appropriate type),
and if not equal to zero, the `true' branch is taken (in the loops,
iteration continues), otherwise the `false' branch is taken (in the
loops, iteration stops).  No value is returned (not even one of type
void).

C's boolean negation operator `!' requires as its operand the same sort
of expression.  The value is compared (with conversion as above) against
zero; if not zero, the result is <int,0> (`false'); otherwise the result
is <int,1> (`true').  (Thus, ! converts not-0 to 0 and 0 to 1.)

C's logical operators <, <=, >, >=, ==, !=, &&, and || work in the
obvious fashion and return either <int,1> (representing `true') or
<int,0> (representing `false').  && and || further guarantee that the
left operand is fully evaluated before the right operand is begun,
and that the evaluation then stops if the result is known (0 for &&,
1 for ||).

Thus, in one respect, the value of `true' is <int,1>.  No other value
is produced for true conditional expressions.  From another point of
view, however, the value of `true' is any non-zero arithmetic expression
or any non-nil pointer expression.  In as few words as possible:

	any nonzero value is taken as true;
	true is given as 1.

>What does if (a=3) evaluate to?

It assigns 3 to a and executes the `true' half of the control flow.
It does not produce a value.
-- 
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