C Program to Swap Two Numbers Using Pointers

In this program, you will learn to swap two numbers using pointers.



Source Code
#include<stdio.h>
#include<conio.h>
void main()
{
    int a,b,*p1,*p2,temp;
    clrscr();
    printf("Enter the value of a:");
    scanf("%d",&a);
    printf("Enter the value of b:");
    scanf("%d",&b);
    printf("\nValues before swapping are: a=%d b=%d", a, b);
    p1=&a;
    p2=&b;
    temp=*p1;
    *p1=*p2;
    *p2=temp;
    printf("\nValues after swapping are: a=%d b=%d", a, b);
    getch();
}
Output
Enter the value of a:65
Enter the value of b:73

Values before swapping are: a=65 b=73
Values after swapping are: a=73 b=65