Associativity -- what is it?

cory at upba.UUCP cory at upba.UUCP
Fri Feb 26 03:13:00 AEST 1988


(Sorry for posting instead of mailing, I couldn't find a path to mccc)

> 	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 ????

I can't really give you an understanding of associativity, as I don't know
what you are missunderstanding.  I could quote the definition, but I don't
think that would solve your problem.  I CAN try to help you understand the
concept of precedence in expression evaluations.  You seem to be confused
on the use of "var ++".  First, think of precedence as just putting a lot
of parentheses everywhere.  You know that ++ has a higher precedence than
*, so lets rewrite your example with parentheses to show the precedence:
	 	x = ( 3 * ( i ++ ) );
Now lets evaluate the expression as it is written.  First the inner-most
parentheses are evaluated:
	EXP=	( i ++ )
This reads "get the value of i, increment i, and return the pre-increment
value".  The value of this part of the expression is the before-increment
value of i.  Now lets continue to the next level of parentheses:
	EXP=	( 3 * EXP )
This says multiply 3 times the value of the inner expression. (which was
the pre-increment value of i)  Finally:
	 	x = ( 3 * ( i ++ ) );
increments i by 1 unit of i's variable type and then assigns x the value
of 3 times the pre-increment value of i. (guzzuntite)

Hope this has been some help.

					-Cory

...!ihnp4!upba!cory (Cory Dekker @ United Phone Book Advertisers)| DISCLAIMER:
Work: 1221 N St, Suite #800; Lincoln, NE 68508 {Ph: 402-476-2200}| My posting..
Home: 800 Foxcroft Ct, #188; Lincoln, NE 68510 {Ph: 402-483-7761}| MY opinions!



More information about the Comp.lang.c mailing list