C Program to swap two numbers using pointers


In this program, You will learn how to swap two numbers using pointers in c.


Before swap: 15 10

After swap: 10 15


Example: How to swap two numbers using pointers in c

Run it Now
#include<stdio.h>

int main() {

   int x, y, temp;
   int *a, *b;

   a = &x;
   b = &y;

   printf("Enter Two Numbers :");
   scanf("%d%d", &x, &y);

   temp = *a;
   *a = *b;
   *b = temp;

   printf("\nAfter Swap x is :%d", x);
   printf("\nAfter Swap y is :%d", y);

   return 0;
}

Output:

Enter Two Numbers :12 13
After Swap x is :13
After Swap y is :12