swap(x,y)
R. Vuurboom
roelof at idca.tds.PHILIPS.nl
Wed Aug 23 03:08:11 AEST 1989
In article <MYwDEFy00WB842JEUx at andrew.cmu.edu> tg1e+ at andrew.cmu.edu (Timothy R. Gottschalk) writes:
>
> To swap two variables x,y in C without using a temporary variable:
>
> /* begin swap */
> x += y;
> y = x - y;
> x -= y;
> /* end swap */
>
> Just curious...(this looks pretty valid) does anyone know an even
>simpler method? I would never program this way -- it's more of a theory
>question. I've been told that it can't be done (???).
^^^
hey ma, thars one o dem
trigraphs again...:-)
This algorithm caught me out once. Try the following:
#define swap(x,y) { x += y; \
y = x-y; \
x -= y; }
main()
{
int i = 3;
int j = 4;
swap(i,j);
printf ("i = %d j = %d\n",i,j);
i = 3;
swap(i,i);
printf ("i = %d\n",i);
}
--
I don't know what the question means, but the answer is yes...
KLM - Koninklijke Luchtvaart Maatschappij => coneenclicker lughtfart matscarpie
Roelof Vuurboom SSP/V3 Philips TDS Apeldoorn, The Netherlands +31 55 432226
domain: roelof at idca.tds.philips.nl uucp: ...!mcvax!philapd!roelof
More information about the Comp.lang.c
mailing list