Associativity -- what is it?

Dave Turner dmt at ptsfa.UUCP
Thu Feb 25 11:45:09 AEST 1988


In article <3466 at killer.UUCP> jfh at killer.UUCP (The Beach Bum) writes:
>In article <234 at mccc.UUCP> pjh at mccc.UUCP (Peter J. Holsberg) writes:
>
>
>pre-fix:	++ X	->	(X = X + 1)
>post-fix:	X ++	->	((X = X + 1), X - 1)
>
>Try this:
>		x = 5;
>		printf ("3 * x ++ = %d\n", 3 * x ++);
>		printf ("now x = %d\n", x);
>
>You should get 15 and 4.  I suspect you expected 18 or 16 or something
>like that.
>


I expected to get 15 and 6 (not 4) which is exactly what I got when I tried it.

I've always found it to be useful to look at ++x and x++ and say:

	++x	increment x before using it in another expression in the
		same statement

and

	x++	use the present value of x in any other expressions in this
		statement before incrementing x.
		X in the next statement will have the new value.

There's more to be said but the above has been satisfactory for over 11 years.


-- 
Dave Turner	415/542-1299	{ihnp4,lll-crg,qantel,pyramid}!ptsfa!dmt



More information about the Comp.lang.c mailing list