中间变量方法:
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int tem = 0;
scanf("%d %d", &a, &b);//输入时两个数中间加空格,因为代码中两个%d中间有空格
printf("a=%d,b=%d\n", a, b);
tem = a;
a = b;
b = tem;
printf("a=%d,b=%d\n", a, b);
return 0;
}
或函数方法:(指针)
#include <stdio.h>
void SWAP(int* pa, int* pb)
{
int tem = 0;
tem = *pa;
*pa = *pb;
*pb = tem;
}
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
printf("a=%d,b=%d\n",a,b);
SWAP(&a,&b);//为什么要用指针,因为void+函数无返回值,不加指针无法对a,b的值进行更改
printf("a=%d,b=%d\n",a,b);
return 0;
}