Introduction :
Swapping two numbers is a simple task in programming. Swapping numbers is used to manipulate data in various programs. Here, you have to exchange values of two variables. In C programming language, you can swap two numbers in several ways such as using a temporary variable, using arithmetic operations and using bitwise operators (XOR).
In this article, I shall show you how to swap two numbers in C programming language. Here, I show you three different examples to swap numbers in C programming language.
How run all the programs :
First, install VS Code on your PC. Then, open VS Code and create new file like “pac.c”. After that, copy the below code and paste in the “pac.c” file. Now, save all the file and run all the program on your PC.
All the program :
Here, I show you three examples by which you can swap two numbers in C programming language. Here, I use printf() function in all my programs.
Example 1 :
Using a temporary variable, you can swap two numbers in C programming language. This is simple and easy way to swap two numbers. In this program, you can use a temporary variable to swap two numbers. Here, you have to store one of the values in a temporary variable before swapping.
Source code :
/*Developed by Puskar Jasu*/
#include <stdio.h>
int main()
{
int first_num = 5, second_num = 10, temp;
printf("The value of first_num = %d and second_num = %d\n", first_num, second_num);
temp = first_num;
first_num = second_num;
second_num = temp;
printf("After swapping using a temporary variable, the value of first_num = %d and second_num = %d\n", first_num, second_num);
return 0;
}
Output :
When you run the program, you can see the output like below image.

Example 2 :
Without using a temporary variable, you can swap two numbers in C programming language. You can use arithmetic operations for this purpose. You can swap two numbers using addition and subtraction operators in C programming language.
Source code :
/*Developed by Puskar Jasu*/
#include <stdio.h>
int main()
{
int first_num = 20, second_num = 50;
printf("The value of first_num = %d and second_num = %d\n", first_num, second_num);
first_num = first_num + second_num;
second_num = first_num - second_num;
first_num = first_num - second_num;
printf("After swapping using arithmetic operations, the value of first_num = %d and second_num = %d\n", first_num, second_num);
return 0;
}
Output :
After run the program on your PC, you can see the output.

Example 3 :
This is an another example swap two numbers without using a temporary variable in C programming language. Here, you have to use XOR bitwise operator to swap two numbers in C programming language.
Source code :
/*Developed by Puskar Jasu*/
#include <stdio.h>
int main()
{
int first_num = 15, second_num = 25;
printf("The value of first_num = %d and second_num = %d\n", first_num, second_num);
first_num = first_num ^ second_num;
second_num = first_num ^ second_num;
first_num = first_num ^ second_num;
printf("After swapping using XOR bitwise operator, the value of first_num = %d and second_num = %d\n", first_num, second_num);
return 0;
}
Output :
When you run the program on your PC, you can see the output of the program like below image.

Conclusion :
At last, you have learned how to swap two numbers in the C programming language. You can use the above programs on your projects. Thank you for visiting my site.