C Program to Swap Two Numbers

Here is a C program to swap two numbers. Swapping two numbers means interchanging the values of two variables.
For Example
Before swapping
A = 3, B = 4
After swapping
A = 4, B = 3

We have to take two numbers as input from user and stores them in two integer variables. There are various ways of swapping values like using a temporary variable, using XOR bitwise operator and without using any temporary variable. Here we will discuss about various ways of swapping and their pros and cons.


C program to swap two numbers using third variable

This program uses a third variable temp of type int. We will use this variable to temporarily store the value of first number. Then we copy second variable in to first variable. As we have saved the value of first variable in temp, we can reassign it not to second variable.
This is the safest way to swap two variables.

#include <stdio.h>

int main(){
    int firstNumber, secondNumber, temp;
    printf("Enter two numbers \n");
    scanf("%d %d", &firstNumber, &secondNumber);
    printf("First Number: %d\nSecond Number: %d\n", 
        firstNumber, secondNumber);   
        
    temp = firstNumber;
    firstNumber = secondNumber;
    secondNumber = temp;
    
    printf("\nAfter Swap\n");
    printf("First Number: %d\nSecond Number: %d\n", 
        firstNumber, secondNumber);
    return 0;
}
Output
Enter two numbers 
2 5
First Number: 2
Second Number: 5

After Swap
First Number: 5
Second Number: 2

C program to swap two numbers without using temporary variable

We first get the sum in one of the two given numbers. The numbers can then be swapped using the sum and subtraction from sum. There is one problem in this approach, the sum of both numbers may overflow the range of integer, in that case we will get wrong values.


#include <stdio.h>

int main(){
    int firstNumber, secondNumber;
    printf("Enter two numbers \n");
    scanf("%d %d", &firstNumber, &secondNumber);
    printf("First Number: %d\nSecond Number: %d\n", 
        firstNumber, secondNumber);
       
    firstNumber = firstNumber + secondNumber;
    secondNumber = firstNumber - secondNumber;
    firstNumber = firstNumber - secondNumber;
    
    printf("\nAfter Swap\n");
    printf("First Number: %d\nSecond Number: %d\n", 
         firstNumber, secondNumber);
    
    return 0;
}
Output
Enter two numbers 
5 6
First Number: 5
Second Number: 6

After Swap
First Number: 6
Second Number: 5

C program to swap two numbers using pointers

In this program we are using a user defined function 'swap' that takes the address of two numbers and swaps their values using a third pointer variable as temporary buffer. This program swaps the values stored in particular memory locations not the references.

#include <stdio.h>

void swap(int *firstNumber, int *secondNumber);
int main(){
    int firstNumber, secondNumber;
    printf("Enter two numbers \n");
    scanf("%d %d", &firstNumber, &secondNumber);
    printf("First Number: %d\nSecond Number: %d\n", 
        firstNumber, secondNumber);   
    
    swap(&firstNumber, &secondNumber);
    
    printf("\nAfter Swap\n");
    printf("First Number: %d\nSecond Number: %d\n", 
        firstNumber, secondNumber);
    return 0;
}

void swap(int *firstNumber, int *secondNumber){
   int temp = *firstNumber;
   *firstNumber = *secondNumber;
   *secondNumber = temp;
}
Output
Enter two numbers 
2 3
First Number: 2
Second Number: 3

After Swap
First Number: 3
Second Number: 2

C program to swap two numbers using bitwise XOR operator

We can use bitwise XOR operator to swap two variables. The XOR of two numbers x and y returns a number which has all the bits as 1 wherever bits of x and y differ. Now, doing Xor again with any of the variable with return other variable.


#include <stdio.h>
 
int main(){
    int firstNumber, secondNumber;
    printf("Enter two numbers \n");
    scanf("%d %d", &firstNumber, &secondNumber);
    printf("First Number: %d\nSecond Number: %d\n",
        firstNumber, secondNumber);  
   
    firstNumber = firstNumber ^ secondNumber;
    secondNumber = firstNumber ^ secondNumber;
    firstNumber = firstNumber ^ secondNumber;
   
    printf("\nAfter Swap\n");
    printf("First Number: %d\nSecond Number: %d\n",
        firstNumber, secondNumber);
    return 0;
}
Related Topics
C program to swap two strings
C program to add n numbers
C program to add two numbers using pointers
C program to check year is leap year or not
C program to add two complex numbers
C program to add digits of a number
C Program to calculate factorial of a number
List of all C programs