C Program to Swap Two Numbers using Pointer

Write a C program to swap two numbers using pointer and the temporary variable. In this example, the swapTwo function accepts two pointer variables integer types. Next, using the temporary variable, we swapped them.

#include <stdio.h>

void swapTwo(int *x, int *y)
  int temp;
  temp = *x;
  *x = *y;
  *y = temp;

int main()
  int num1, num2;

  printf("Please Enter the First Value to Swap  = ");
  scanf("%d", &num1);

  printf("Please Enter the Second Value to Swap = ");
  scanf("%d", &num2);

  printf("\nBefore Swapping: num1 = %d  num2 = %d\n", num1, num2);
  swapTwo(&num1, &num2);

  printf("After Swapping : num1 = %d  num2 = %d\n", num1, num2);

C Program to Swap Two Numbers using Pointer

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.