Swap two variables using macro

Ziaur Rahman's Avatar author of Swap two variables using macro
This is an article on Swap two variables using macro in C.
The code to swap two variables using macro expansions

Code: C
#include<stdio.h>
#include<conio.h>
#define SWAPE(x,y) int t;t=x;x=y;y=t;
main()
{
    int a,b;
   
    printf("\n Enter two number");
    scanf("%d%d",&a,&b);
    printf("\n Before swaping the Value of a=%d and b=%d",a,b);
    SWAPE(a,b);
    printf("\n After swap value of a=%d and b=%d",a,b);
    return 0;
}
Newbie Member
2Nov2006,13:35   #2
nicenaidu's Avatar
//Code for swapping two numbers without using temp variable
Code: CPP
#include <iostream>
using namespace std;
int main ()
{
  int a = 10;
  int b = 5;

  cout << "before swap: a = " << a << " b = " << b << endl;
 
  //Swapping numbers without using a temp var : method1
  a = a ^ b;
  b = b ^ a;
  a = a ^ b;

  cout << "after swap using method1: a = " << a << " b = " << b << endl;

  //Swapping numbers without using a temp var : method2
  a = a + b;
  b = a - b;
  a = a - b;
  cout << "after swap using method2: a = " << a << " b = " << b << endl;

  return 0;
}
Light Poster
22Dec2006,14:42   #3
aurnk's Avatar
better method is
usage of
#define swap(a,b) a^=b^=a^=b;
Light Poster
22Dec2006,14:43   #4
aurnk's Avatar
better method is
usage of
#define swap(a,b) a^=b^=a^=b;

one line swap without using temporary variable.
Contributor
12Apr2007,11:15   #5
Peter_APIIT's Avatar
Hello nicenaidu, i have compiler the file you post in MS VS 2005 but the IDE complaint that cstiod.h error .

Thanks for your help.


Your help is greatly appreciated by me and others.
Newbie Member
24May2007,15:43   #6
Vikrant Singh's Avatar
Here is a suggestion...

Using ^ operator is not the best way of swapping two values.
Just consider is soemone try to SWAP same variable, like SWAP(A,A) what will happen?
A^A will clear the all of the bits of A, because of which A will lost its original value on calling SWAP macro.

Further swapping value using equation like A+B is not advisable , if A and B are very large values than result of this addition may cross the maximum value which an integer can store. We may end up with a incorrect result.

So we should not try to play smart with fancy instruction as far as swapping is concerned.
Go4Expert Member
25May2007,13:30   #7
oleber's Avatar
Lets speak of C/C++

Why use a Macro and fix a type?

Template functions can do the work cleaner.

Code: CPP
template <class TYPE>
void swamp(TYPE &var1, TYPE &var2) {
    TYPE var_temp = var1;
    var1 = var2;
    var2 = var_temp;
}
Newbie Member
25May2007,18:59   #8
Vikrant Singh's Avatar
This can be done in Macro also…
Try this…
#define SWAP (A, B) struct tempStruct { char C[sizeof(A)];} swap_tmp;\
swap_tmp = *( struct tempStruct*) &A;\
*( struct tempStruct*) &A = *( struct tempStruct*) &B;\
*( struct tempStruct*) &B = swap_tmp;
Go4Expert Member
25May2007,20:04   #9
oleber's Avatar
Correct to

Code:
    #define SWAP(A, B) {struct tempStruct { char C[sizeof(A)];} swap_tmp;\
    swap_tmp = *( struct tempStruct*) &A;\
    *( struct tempStruct*) &A = *( struct tempStruct*) &B;\
    *( struct tempStruct*) &B = swap_tmp;}
Other wise you get problems when using diferent types

Code: CPP
#include <cstdio>
    #include <iostream>
   
    using namespace std;
   
    #define SWAP(A, B) {struct tempStruct { char C[sizeof(A)];} swap_tmp;\
    swap_tmp = *( struct tempStruct*) &A;\
    *( struct tempStruct*) &A = *( struct tempStruct*) &B;\
    *( struct tempStruct*) &B = swap_tmp;}
   
    int main () {
       
        char v1 = 'a';
        char v2 = 'b';   
        SWAP (v1, v2);
        cout << v1 << "   " << v2 <<"\n";
   
        float f1 = 45;
        float f2 = 123;
        SWAP (f1, f2);
        cout << f1 << "   " << f2 <<"\n";
    }

Last edited by oleber; 25May2007 at 20:32..
Newbie Member
4Jul2007,15:39   #10
keith12125's Avatar
>better method is
>usage of
>#define swap(a,b) a^=b^=a^=b;
>
>one line swap without using temporary variable

what exactly does the assignment operator ^= do?