C Program to Swap Two Numbers using Pointer

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

#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