swap(x,y)

Timothy R. Gottschalk tg1e+ at andrew.cmu.edu
Tue Aug 22 16:34:57 AEST 1989


       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 (???).
Tim Gottschalk
Pgh, PA



More information about the Comp.lang.c mailing list