C语言使用指针交换两个数字的程序

编写一个C程序,使用指针和临时变量交换两个数字。在此示例中,swapTwo函数接受两个整数类型的指针变量。接下来,我们使用临时变量交换它们。

#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);

}
Program to Swap Two Numbers using Pointer