When is a statement an expression?

The Cybermat Rider laba-4he at web-4a.berkeley.edu
Fri Apr 28 09:03:41 AEST 1989


In article <1127 at ptolemy.arc.nasa.gov> raymond at ptolemy.arc.nasa.gov (Eric A. Raymond) writes:
>In article <1043 at itivax.iti.org> scs at vax3.iti.org (Steve Simmons) writes:
>>	a = if ( a == 1 )
>>		12 ;
>>	else
>>		14 ;
[....]
>You can accomplish this behavior via the ?: connstruct:
>
>      a = (a ? 12 : 14);
       ^^^^^^^^^^^^^^^^^^
Close, but not quite -- if a is 2, you'll get *12*, not 14.  It should be:

	a = ((a == 1) ? 12 : 14);
>
>Incidently, I beleive the comma operator allows you to approach a
>progn (or is it prog1?):
>
>      a = (x=1, y=2, z=3);	
>
>A is 3 if the last expr is returned (progn-like), otherwise 1
>(prog1-like).  Look it up.

I did.  Sorry, but a is guaranteed to be 3.  To quote K&R 2:

A7.18	Comma Operator

	expression:
		assignment-expression
		expression , assignment-expression

A pair of expressions separated by a comma is evaluated left-to-right, and
the value of the left expression is discarded. [.....]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>-- 
>Eric A. Raymond  (raymond at ptolemy.arc.nasa.gov)
>G7 C7 G7 G#7 G7 G+13 C7 GM7 Am7 Bm7 Bd7 Am7 C7 Do13 G7 C7 G7 D+13: Elmore James

----------------------------------------------------------------------------
Adrian Ho a.k.a. The Cybermat Rider	  University of California, Berkeley
laba-4he at web.berkeley.edu		(WEB Evans, Home of The CS Freakies)
Disclaimer:  Nobody takes me seriously, so is it really necessary?



More information about the Comp.lang.c mailing list