Obfuscated SWAP: not portable!
Conor P. Cahill
cpcahil at virtech.UUCP
Thu Sep 7 00:01:13 AEST 1989
In article <1400 at levels.sait.edu.au>, CCDN at levels.sait.edu.au (DAVID NEWALL) writes:
> In article <149 at cpsolv.UUCP>, rhg at cpsolv.UUCP (Richard H. Gumpertz) writes:
> > The exchange of x and y that has been under discussion:
> > x ^= y ^= x ^= y
> > is non-portable because it depends on right-to-left evaluation which is NOT
> > specified in the C standard.
>
> I think it is specified. (Perhaps it depends on whose standard you follow?)
>
> K&R I, page 19:
> "The line
> nl = nw = nc = 0;
> sets all three variables to zero. This is not a special case, but a
> consequence of the fact that an assignment has a value and assignments
> associate right to left."
But, you cannot know what the value of x is at the far left of the
equation prior to evaluating the "^=". It may be the original value
of x, or it may be the latest value of x (modified by the right hand
side of the equation. As far as I know, there is no specification
as to when the x will be evaluated.
--
+-----------------------------------------------------------------------+
| Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 !
| Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 |
+-----------------------------------------------------------------------+
More information about the Comp.lang.c
mailing list