Increment Operators vs. Precedence

Scott Horne horne-scott at cs.yale.edu
Thu Mar 7 06:46:22 AEST 1991


In article <THOMSON.91Mar5173421 at zazen.macc.wisc.edu> thomson at zazen.macc.wisc.edu (Don Thomson) writes:
<
<The example on the board is y = x++, and I explain that x gets
<assigned to y before x is incremented.  An astute individual in the back of
<the room raises his hand and points out that according to what I just taught
<them, the precedence is wrong, that in fact the assignment operator has a
<significantly lower precedence than ++.  So the dilemma is how to explain that
<precedence is not the issue here, that the order of operations is tied to the
<definition of prefix versus postfix increment operators.  Any ideas on how to
<word the explanation in such a way that I don't leave them forever suspicious
<about what does and what does not have to obey precedence rules?

Suppose you have the following:

	int     x = 37,
	        y;

	y = x++;

`x++' is an expression with the value 37.  It has the side effect of setting
the value of `x' to 38.  This evaluation does occur before the assignment of
37 to `y', but it doesn't affect `y'.

`++x' has the value 38.  It happens to have the side effect of setting `x'
to the same value.

					--Scott

-- 
Scott Horne                               ...!{harvard,cmcl2,decvax}!yale!horne
horne at cs.Yale.edu      SnailMail:  Box 7196 Yale Station, New Haven, CT   06520
203 436-1817                    Residence:  Rm 1817 Silliman College, Yale Univ
Uneasy lies the head that wears the _gao1 mao4zi_.



More information about the Comp.lang.c mailing list