Swapping Program in C

There are many different swapping program in C language. Swap two numbers mean to exchange the values of two variables with each other. X=10 and Y=20 then after swapping X=20 and Y=10


Swapping program in C using a temporary variable

#include<stdio.h>
int main()
{
    int a=10, b=20, c;
    c = a;
    a = b;
    b = c;
    printf("a=%d \t b=%d\n",a,b);
    return 0;
}

Output:-

a=20 b=10

Explanation:-
At starting we take a=10, b=20 and the temporary variable c for the swap. We can take the value of a & b from the user also, try it yourself.

Initially, a=10, b=20

c = a; c=10 Now, a=10, b=20, c=10
a = b; a=20 Now, a=20, b=20, c=10
b = c; b=10 Now, a=20, b=10, c=10

Finally a=20, b=10

Swapping program in C without using any temporary variable

Swapping program in C using arithmetic operators + and –

#include<stdio.h>
int main()
{
    int a=10, b=20;
    a = a + b;
    b = a - b;
    a = a - b;
    printf("a=%d \t b=%d\n",a,b);
    return 0;
}

Output:-

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

a=20 b=10

Explanation:-
At starting a=10 , b=20

a = a + b; a= 10+20 = 30 Now, a=30 , b=20
b = a – b; b= 30-20 = 10 Now, a=30 , b=10
a = a – b; a= 30-10 = 20 Now, a=20 , b=10

Finally a=20, b=10

The above code can be written in one line,

a = (a+b) - (b=a);

The above method works well, but it may cause arithmetic overflow when adding large numbers.

Swap Two Number using arithmetic operators * and /

a= a*b;
b= a/b;
a= a/b;

Let a=5 and b=4
Then, a= a*b = 54=20 so, a=20 b=4
Now, b= a/b = 20/4=5 so, a=20 b=5
Again,a= a/b = 20/5=4 so, a=4 b=5
Finally a=4 and b=5

Above code can be written in one line,

a = (a*b) / (b=a);

Note:- This method will not work if any variable value is zero. Because the product becomes zero. It may also cause an arithmetic overflow when multiplying large numbers.

Swap Two Number using bitwise XOR operator ^

We can swap two numbers using the XOR operator. The result of the bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It is denoted by ^.

x = x^y ;
y = x^y ;
x = x^y ;

This method is a bit easy because it uses only the bitwise operator.
It can also be written as,

x ^= y;
y ^= x;
x ^= y;

This method can be written in one line. Order of evaluation is from right to left.

x ^= y ^= x ^= y;

Let At starting, x=12 and y=20 so,
x=12=1100(in binary) and y=20=10100(in binary)

x = x^y ; 01100^10100=11000=24, now x=24, y=20
y = x^y ; 11000^10100=01100=12, now x=24, y=12
x = x^y ; 11000^01100=10100=20, now x=20, y=12

Finally, x=20, y=12

Using + and – in a slightly different way

a = b - a;
b = b - a;
a = b + a;

Note that the corresponding method with * and / operators does not work.

Using Bitwise Complement (~) & arithmetic operators (+, -)

a = b-~a-1;
b = a+~b+1;
a = a+~b+1;

Swap Two Number using a macro

#include<stdio.h>
#define SWAP(a,b,Type) Type temp=a; a=b; b=temp;
int main()
{
  int x=14, y=15;
  SWAP(x,y,int);
  printf("After Swapping \n x=%d \t y=%d\n",x,y);
  return 0;
}

The same SWAP macro will work for types like int, float, and char. By using this GCC-specific extension, we can improve it further like:

#define SWAP(a,b) Typeof(a) temp=a; a=b; b=temp;

and use SWAP(x,y); in place of SWAP(x,y,int);

The problem will arise in this method when there is already a variable named temp in the program!

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *