Associativity -- what is it?

Bill Crews bc at halley.UUCP
Wed Feb 24 12:40:28 AEST 1988


In article <234 at mccc.UUCP> pjh at mccc.UUCP (Peter J. Holsberg) writes:
>In article <224 at sdrc.UUCP> scjones at sdrc.UUCP (Larry Jones) writes:
>|In article <226 at mccc.UUCP>, pjh at mccc.UUCP (Peter J. Holsberg) writes:
>|> 
>|> 	x = 3 * i ++;
>|> 
>|> Book says that ++ has a higher precedence than *, and that ++
>|> associates from R->L.  That makes me think that ++ should be applied
>|> first, but I know it isn't.  But ????
>|
>|But ++ IS applied first!  The key point here is that the RESULT of postfix
>                                                          ^^^^^^^^^^^^^^^^^
>|++ is the value BEFORE incrementation, not that postfix ++ is somehow deferred
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>|until later.
>
>Well, that still leaves me confused.  If i has the value 7, it is 7 that
>is added to 3, so it seems to be that the ++ *is* deferred until later. 
>Also, ++ has higher precedence than +, so why is the incrementation
>delayed until after the current value of i is used?

Here is how to think of it.  ++ performs two functions.  It returns a value,
and it has an effect on the variable to which it is applied.

If the ++ precedes its operand:

	it increments the operand
	it returns the resulting value

If the ++ succeeds its operand:

	it increments the operand
	it returns the value of the operand prior to the increment

The other operators with which you are comparing it have only the functions of
returning a value.  This makes it confusing.  But the act of returning the
previous value is quite different from deferring the operator's effect.

-bc
-- 
Bill Crews                                   Tandem Computers
bc at halley.UUCP                               Austin, Texas
..!rutgers!im4u!halley!bc                    (512) 244-8350



More information about the Comp.lang.c mailing list