Before I provide you working sample code, let's understand the basic of program, for example, we have two numbers(10, 20) saved in two variables (firstNumber=10 & secondNumber=20), now we need to interchange these two numbers, here interchanging means at the end of program, firstNumber =20 and secondNumber =10, so to do that we would need thrid variable Temp, to store a value temaporarily.

We can also do swapping of two numbers without using third variable at all.

Let's get started to create swapping program with different methods one by one.

C program for swapping of two numbers using temporary variable

#include<stdio.h>
 
void main()
{
  int firstNumber, secondNumber, Temp;
 
  //get two number from user and save it in variable firstNumber, secondNumber
  printf("\nPlease Enter the value of First Number and Second Number\n");
  scanf("%d %d", &firstNumber, &secondNumber);
 
  //print numbers before swapping them (example: firstNumber=10, secondNumber =20)
  printf("\nBefore Swapping First Number = %d and Second Number = %d\n", firstNumber,secondNumber);
 
  //save firstNuumber in temp variable (Ex: Temp= 10)
  Temp = firstNumber;

  //get secondNumber in firstNumber now (Ex: firstNumber =  20)
  firstNumber = secondNumber;

  //get the temp value in second now (Ex: secondNumber = 10 as Teamp =10)
  secondNumber = Temp;
 
//print swapped numbers
  printf("\nAfter Swapping: First Number = %d and Second Number = %d\n", firstNumber, secondNumber );
 
  
}

I have explained important lines of the code using comment's. Executing the above program here https://onlinegdb.com/H1PgvFrgQ gives me output as below

Please Enter the value of First Number and Second Number                                       
10 20                                           
                                                                            
Before Swapping First Number = 10 and Second Number = 20               
                                                                                                
After Swapping: First Number = 20 and Second Number = 10

swapping-two-numbers-in-c-using-temp-min.png

C program to swap two strings using pointers and functions

#include <stdio.h>

//define function to swap values
void swap(int *a , int *b)
{
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}
void main()
{
    int firstNumber, secondNumber;
    
   //get two number from user and save it in variable firstNumber, secondNumber
    printf("\nPlease Enter the value of First Number and Second Number\n");
    scanf("%d %d", &firstNumber, &secondNumber);
    
    //print numbers before swapping them (example: firstNumber=10, secondNumber =20)
     printf("\nBefore Swapping First Number = %d and Second Number = %d\n", firstNumber,secondNumber);
     
      //call swapping function by passing reference of variables
    swap(&firstNumber, &secondNumber);
    
     //print swapped numbers
     printf("\nAfter Swapping: First Number = %d and Second Number = %d\n", firstNumber, secondNumber );
}

In this program the two numbers to be swapped from each other place with the use of pointer this concept works on the address reference. In program the variables declare for storing the value in it. Here a variable named ‘temp’ also declared with the use of only two numbers the swapping will happen interference of temp variable the first number shifted to temp and then to other variable the reverse phase will be on the move to swap both numbers.

Output of the above program

Please Enter the value of First Number and Second Number                          
10 20                         
                                                                                                                         
Before Swapping First Number = 10 and Second Number = 20                                
                                                                                                                   
After Swapping: First Number = 20 and Second Number = 10

swap-numbers-in-c-using-pointers-functions-min.png

C program to swap numbers without using third (temporary) variable

#include <stdio.h>
 void main()
{
   int firstNumber, secondNumber;
 
  // get two number from user and save it in variable firstNumber, secondNumber
  printf("\nPlease Enter the value of First Number and Second Number\n");
  scanf("%d %d", &firstNumber, &secondNumber);
 
  //print numbers before swapping them (example: firstNumber=10, secondNumber =20)
  printf("\nBefore Swapping First Number = %d and Second Number = %d\n", firstNumber,secondNumber);
 
   firstNumber = firstNumber + secondNumber;
   secondNumber = firstNumber - secondNumber;
   firstNumber = firstNumber - secondNumber;
 
   //print swapped numbers
  printf("\nAfter Swapping: First Number = %d and Second Number = %d\n", firstNumber, secondNumber );
   
}

In the above program, we are first adding both the variable, saving it in firstNumber and then subtracting secondNumber from firstNumber, gives us swapped value as firstNumber, similarly we get secondNumber also. Consider this example below:

  1. firstNumber = 10, SecondNumber =30
  2. executing first line, firstNumber = firstNumber + secondNumber; gives firstNumber = 10 +  30 =40
  3. executing second line, secondNumber = firstNumber - secondNumber; gives secondNumber =40 - 30  =10
  4. executing third line, firstNumber = firstNumber - secondNumber; gives firstNumber = 40-10 = 30;
  5. thus, at the end of the program we get  firstNumber = 30, SecondNumber =10
Please Enter the value of First Number and Second Number                                  
30 40                                                                   
                                              
Before Swapping First Number = 30 and Second Number = 40                          
                                                                                                               
After Swapping: First Number = 40 and Second Number = 30

swap-two-numbers-without-using-third-variable-min.png

C program to swap integer using bitwise XOR

#include <stdio.h>
 void main()
{
   int firstNumber, secondNumber;
 
  // get two number from user and save it in variable firstNumber, secondNumber
  printf("\nPlease Enter the value of First Number and Second Number\n");
  scanf("%d %d", &firstNumber, &secondNumber);
 
  //print numbers before swapping them (example: firstNumber=10, secondNumber =20)
  printf("\nBefore Swapping First Number = %d and Second Number = %d\n", firstNumber,secondNumber);
 
   firstNumber = firstNumber ^ secondNumber;
   secondNumber = firstNumber ^ secondNumber;
   firstNumber = firstNumber ^ secondNumber;
 
   //print swapped numbers
  printf("\nAfter Swapping: First Number = %d and Second Number = %d\n", firstNumber, secondNumber );
   
}

Above program usese bitwise operator (^) to swap variable without using third variable, with same logic as we did in third program. Executing the above program online here https://onlinegdb.com/SkYPkjHxX gives output as below

Please Enter the value of First Number and Second Number                                          
10 50                                                     
Before Swapping First Number = 10 and Second Number = 50                             
                                                                                       
After Swapping: First Number = 50 and Second Number = 10

swap-number-using-bitwise-min.png

You may also like:

Program for insertion sorting in C (With explanation)

Linear search program in C (Various ways explained with example)

Program for Matrix multiplication in C (With & Without pointers)


If you have any question's related to the above article, feel free to comment in the comment's section or link this question and ask a question in forum section.