Swapping two variables without using a temp variable

Last update on August 9, 2009 03:04 PM by jak58
Published by jak58

Swapping two variables without using a temp variable






Using pointers


#include <stdio.h>
void change(int *,int*);

int main ()
{
  int a=2,b=5;
  printf("Before : a=%d,b=%d\n",a,b);

  change(&a,&b);

  printf("After : a=%d,b=%d\n",a,b);
  return 0;
}

void change(int *a,int *b){

  *a += *b;
  *b = *a-*b;
  *a = *a-*b;
}


Results
Before : a=2,b=5
After : a=5,b=2

Making use of a Macro


#include <stdio.h>
#define SWAP(x,y) x ^= y, y ^= x, x ^= y

int main ()
{
  int a=2,b=5;
  printf("Before : a=%d,b=%d\n",a,b);

  SWAP(a,b);

  printf("After : a=%d,b=%d\n",a,b);
  return 0;
}

Results
Before : a=2,b=5
After : a=5,b=2

Note that:
The name of the macro or variables may be changed to your convenience.
Best answers for « Swapping two variables without using a temp variable » in :
Environment variables Show Environment variables An environment variable is a dynamic value loaded into the memory that can be used by several processes operating simultaneously. On most operating systems, the location of some libraries or of the main system executables may...
[VBA/VB6] My Documents + Environment Variables Show [VBA/VB6] My Documents + Environment Variables With VBA With VB6 Environment Functions Windows Variables Getting started As displayed in Windows Explorer, the My Documents folder appears to be in the root, but it is not the case. It...
Deleting the uninstall files for SP1 & 2 under Vista ShowDeleting the uninstall files for SP1 & 2 under Vista This trick allows you to earn more than 700 mb on the system partition by removing the backup files created after installation the service pack (1 and 2). However, after deleting...
Basic Excel Formulas 2 ShowBasic Excel Formulas 2 We saw the SI function Function and is also interesting. Combined with SI, this gives: = IF (AND (condition1; condition2 ;....... conditionZ) true false) displays on the various conditions to be verified (eg...
Call of duty 2 (COD2) Bugs ShowCall of duty 2 (COD2) Bugs Call of duty 2 bugs on VISTA Patch Installation Direct X 10 configurations Multiplayer Mode You have problems running COD 2 on Vista? This is because this FPS game was released before vista So we will...
Download VLC for Mac OS X PowerPC ShowVLC media player is a multimedia player capable of playing many audio and video formats (MPEG-1, MPEG-2, MPEG-4, DivX, mp3, ogg, ...), as well as DVD or VCD and various protocols streaming. It can also be used as a server for streaming unicast or...
Download AVS Media Player ShowAVS media Player is an universal DVD, VIDEO & AUDIO Player: Supporting a very wide variety of video formats : the entire range of MPEG-4 (including DivX, Xvid, etc.), MPEG-1, MPEG-2, AVI, Real Media video, Quick Time files, WMV files (including...
PS/2 connector ShowPS/2 connector The PS/2 connector (mini-DIN6 format) is mainly used to connect computers to keyboards and mice. Pins Pin number Function 1 Clock 2 Ground 3 Data 4 Ground (or not...