swap(x,y)

david.f.prosser dfp at cbnewsl.ATT.COM
Wed Aug 23 08:10:23 AEST 1989


In article <8350 at boring.cwi.nl> tromp at piring.cwi.nl (John Tromp) writes:
>How about
>
>x^=y^=x^=y;
>
>Avoids possible overflow problems and looks great in an
>Obfuscated C Code entry too!

This code need not do what was intended.  In particular, it requires that
"x" be assigned the result of the right-most ^= operation prior to its
value being used in the left-most ^= operation.  To eliminate this
evaluation-order dependent behavior, some form of sequence point must be
used between these two operations.  For example

	y^=x^=y,x^=y;

See section 3.3, and in particular footnote 31 (page 39) of the pANS.

Dave Prosser	...not an official X3J11 answer...



More information about the Comp.lang.c mailing list