From: The Amorphous Mass <robinson@blue.weeg.uiowa.edu>
Newsgroups: comp.lang.c
Subject: Re: a ^= b ^= a ^= b ???
Date: Thu, 21 Sep 1995 15:04:13 -0500
Message-ID: <Pine.A32.3.91.950921145742.43918A-100000@black.weeg.uiowa.edu>
In-Reply-To: <43k3bf$ipk@winx03.informatik.uni-wuerzburg.de>

On 18 Sep 1995, Frank Hofmann wrote:
> I found this thread very interesting so far. That's why I've tried it myself:
>
> void main (void)
> {
>   int a, b;
>   a = 123; b = 7654;
>   printf ("a = %d, b = %d\n", a, b);
>   a^=b^=a^=b;
>   printf ("a = %d, b = %d\n", a, b);
> }

This code crashes my coworker's Toshiba laptop, not because of the ^= bit, but because of the void main(void). We changed it to int main(void) and it worked as you hope it works.

We were running under BC++ 4.5.

However, the fact that it works on every compiler on every platform you can find means little. It works because the compilers are nice enough to correctly guess what you want out of "a^=b^=a^=b," not because it's correct or clear. The result of that expression is undefined; if there are scads of compilers that guess the intended result, then there are a lot of people getting away with sloppy programs.

As someone else has no doubt pointed out, the compiler is as free to overwrite your hard drive as it is to swap the values of a and b in the above program.

Just for the heck of it, I'll see what THINK C 5.0 makes of it.

___________
Bushido, n.: the ancient art of keeping your  | James Robinson
cool when a US President ralphs in your lap.  | james-f-robinson@uiowa.edu