Home »
C programs »
C pointer programs
C program to swap two numbers using pointers
In this C program, we are going to learn how to swap two integer numbers using pointers? Here, we are using the concept of call by reference (also known as call by address).
By IncludeHelp Last updated : March 10, 2024
Given two integer numbers are we have to swap their values using pointers in C language.
Swapping two numbers using C pointers
Here, we are using a function to swap the values swap() - function has two integer pointer type arguments and within the body we are swapping them. Since address of the actual values are passing within the function, swapping will be done with the actual arguments.
Swap two numbers using call by reference (address) in C
/*C program to swap two numbers using pointers.*/
#include <stdio.h>
// function : swap two numbers using pointers
void swap(int *a,int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}
int main()
{
int num1,num2;
printf("Enter value of num1: ");
scanf("%d",&num1);
printf("Enter value of num2: ");
scanf("%d",&num2);
//print values before swapping
printf("Before Swapping: num1=%d, num2=%d\n",num1,num2);
//call function by passing addresses of num1 and num2
swap(&num1,&num2);
//print values after swapping
printf("After Swapping: num1=%d, num2=%d\n",num1,num2);
return 0;
}
Output
Enter value of num1: 10
Enter value of num2: 20
Before Swapping: num1=10, num2=20
After Swapping: num1=20, num2=10
C Pointer Programs »