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;
}
0
nicenaidu's Avatar, Join Date: Nov 2006
Newbie Member
//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;
}
0
aurnk's Avatar, Join Date: Dec 2006
Light Poster
better method is
usage of
#define swap(a,b) a^=b^=a^=b;
0
aurnk's Avatar, Join Date: Dec 2006
Light Poster
better method is
usage of
#define swap(a,b) a^=b^=a^=b;

one line swap without using temporary variable.
0
Peter_APIIT's Avatar, Join Date: Apr 2007
Contributor
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.
0
Vikrant Singh's Avatar
Newbie Member
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.
0
oleber's Avatar, Join Date: Apr 2007
Go4Expert Member
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;
}
0
Vikrant Singh's Avatar
Newbie Member
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;
0
oleber's Avatar, Join Date: Apr 2007
Go4Expert Member
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..
0
keith12125's Avatar, Join Date: Jul 2007
Newbie Member
>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?