用第三方变量
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
int a = 3;
int b = 4;
int tmp = 0;
printf("交换前a= %d\n",a);
printf("交换前b= %d\n",b);
tmp = a;
a = b;
b = tmp;
printf("交换后a= %d\n", a);
printf("交换后b= %d\n", b);
return 0;
}
不用第三方变量
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
int a = 3;//0011
int b = 4;//0100
printf("交换前\n");
printf("a=%d\n", a);
printf("b=%d\n", b);
a = a ^ b;//0111
b = a ^ b;//0011==3
a = a ^ b;//0100==4
printf("交换后\n");
printf("a=%d\n",a);
printf("b=%d\n",b);
return 0;
}
标签:tmp,include,变量,int,交换,printf,main,数据
From: https://blog.51cto.com/u_16425777/8806003