1、用中间变量temp
#include<stdio.h>
int main()
{
printf("请输入两个数字:\n");
int num1=10,num2=20;
scanf("%d%d",&num1,&num2);
int temp=0;
temp=num2;//temp=num1;
num1=temp;//num1=num2;
num2=num1;//num2=temp;
printf("num1=%d,num=%d",num1,num2);
return 0;
}
2、用算数加减法(扩充面试内容)
#include<stido.h>
int main()
{
printf("请输入两个整型:\n");
int num1=10,num2=20;
scanf("%d%d",&num1,&num2);
num1=num1+num2;//num1=30,num2=20
num2=num1-num2;//num1=30,num2=10
num1=num1-num2;//num1=20,num2=10
//实现num1,num2的交换
printf("num1=%d,num2=%d",num1,num2);
return 0;
}
3、用位运算的^异或(注意:是二进制运算)(具体异或的运算方法请看往期文章"运算符")
#include<stdio.h>
int main()
{
printf("请输入两个整型:\n")
int num1=10,num2=20;
scanf("%d%d",&num1,&num2);
num1=num1^num2;
num2=num1^num2;
num1=num1^num2;
//与前面算数方法的过程基本一致
printf("num1=%d,num2=%d",num1,num2);
return 0;
}