In this article, we will write a C program to swap two numbers using pointers.
The program takes two numbers from the user as input, stores these numbers in variables a and b, and swaps their values after executing.
Sample Input:
Enter the value of a and b: 10 20
Output:
Before swapping a = 10 and b = 20
After swapping a = 20 and b = 10
Swapping is the process of interchanging the values of two variables. In other words, swapping the values of two variables means that the value of the first variable is assigned to the second variable and the value of the second variable is assigned to the first variable.
In C programming, there are different ways to swap two numbers. We will use C pointers to swap two numbers.
In C language, pointers are used to store the memory address of other variables. They are declared by putting an asterisk(*
) symbol before the variable name, for eg. *myPtr
.
When we assign a variable to a pointer, the pointer holds the memory address of that variable. So, if we make any changes to the variable, these changes directly reflect on the pointer or vice versa.
The following C program shows how we can swap two numbers using pointers:
// C program to swap two numbers using pointers #include <stdio.h> int main() { int a, b, temp; int *ptr1, *ptr2; printf("Enter the value of a and b: "); scanf("%d %d", &a, &b); printf("\nBefore swapping a = %d and b = %d", a, b); // Assign the memory address of a and b to *ptr1 and *ptr2 ptr1 = &a; ptr2 = &b; // Swap the values a and b temp = *ptr1; *ptr1 = *ptr2; *ptr2 = temp; printf("\nAfter swapping a = %d and b = %d", a, b); return 0; }
Output:
Enter the value of a and b: 10 20 Before swapping a = 10 and b = 20 After swapping a = 20 and b = 10
Code Explanation:
- The program takes two numbers from the user and stores them in
num1
andnum2
variables respectively. - We then store the memory address of
num1
in*ptr1
and the memory address ofnum2
in*ptr2
using the statementsptr1 = &a;
andptr2 = &b;
respectively. - The pointers
*ptr1
and*ptr2
are now directly pointing to variablesa
andb
respectively, therefore, if we make any changes to these pointers, the changes will also reflect on variablesa
andb
. - To swap the values of
a
andb
, we have created a temporary variabletemp
. The variabletemp
is used to hold the value of*ptr1
i.e. value of variablea
. - The statement
*ptr1 = *ptr2;
means that the value of *ptr2 is assigned to *ptr1. - Finally, we copied the old value of *ptr1 to *ptr2 using the
temp
variable i.e.*ptr2 = temp;
.
Example 2: Swap Two Numbers by Passing Pointers to Functions
We can also create a user-defined function which will take the references of the two variables as its arguments and swap their values with the help of pointers.
In the following C program, we have created a function swap()
. The swap()
function takes two arguments which are actually the memory addresses of the first and second variables and performs swapping on calling.
// C program to swap two numbers using pointers #include <stdio.h> // Function declaration void swap(int * , int * ); int main() { int a, b, temp; int *ptr1, *ptr2; printf("Enter the value of a and b: "); scanf("%d %d", &a, &b); printf("\nBefore swapping a = %d and b = %d", a, b); // Call the swap() function to swap a and b swap( &a, &b); printf("\nAfter swapping a = %d and b = %d", a, b); return 0; } // User-defined function to swap two numbers void swap(int * ptr1, int * ptr2) { int temp; temp = * ptr1; * ptr1 = * ptr2; * ptr2 = temp; }
Output:
Enter the value of a and b: 10 20 Before swapping a = 10 and b = 20 After swapping a = 20 and b = 10
Thanks for reading.