Table of Contents
C program to swap two numbers using pointers
In simple terms pointers are the variables which store addresses of another variable.
It can also store addresses of functions, arrays, structures and other pointers along with primitive data type variables.
Logic to swap two numbers using pointers in C
#include<stdio.h>
//itvoyagers.in
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
//itvoyagers.in
int main()
{
int x, y;
printf("Enter first number : ");
scanf("%d",&x);
printf("Enter second number : ");
scanf("%d",&y);
printf("Value of X before swapping = %dnValue of Y before swapping = %d", x, y);
swap(&x, &y);
printf("nValue of X after swapping = %dnValue of Y after swapping = %d", x, y);
return 0;
}
Output :
Enter first number : 5
Enter second number : 8
Value of X before swapping = 5
Value of Y before swapping = 8
Value of X after swapping = 8
Value of Y after swapping = 5
PRACTICALS/PRACTICE PROGRAM IN C
CHECKOUT OTHER RELATED TOPICS
CHECKOUT OTHER QUIZZES
Quizzes on Operator |
Easy Quiz on Operators in C part 1 |
Easy quiz on operators in C part 2 |
Easy quiz on operators in C part 3 |
Quizzes on Loops |
Easy quiz on loops in C part 1 |
Easy quiz on loops in C part 2 |
Easy quiz on loops in C part 3 |
Quizzes on Decision Making Statement |
Easy Quiz On Decision Making Statements Part 1 |
Easy Quiz On Decision Making Statements Part 2 |
Quizzes on String |
Easy quiz on String in C programming part 1 |
Quizzes on Array |
Easy quiz on Array in C programming – part 1 |
5 – Best Java program to convert decimal to binary |
New Quizzes Coming soon.. |
We are aiming to explain all concepts of C in easiest terms as possible.

ITVoyagers
Author