您的位置:首页 > 编程语言 > C语言/C++

C语言交换两个数的值

2014-01-21 10:40 239 查看
#include<stdio.h>
int main()
{
//交换两个数的值
// 方法一 可读性最好
int a = 10;
int b = 11;
int temp ;
temp = a;
a = b;
b = temp;
printf("a = %d, b = %d\n",a, b);

//方法二
int c = 10;
int d = 11;
c = d - c;
d = d - c;
c = d + c;
printf("c = %d, d = %d\n",c, d);
// 方法三
int e = 10;
int f = 11;
e = e ^ f;
f = e ^ f;
e = e ^ f;
printf("e = %d, f = %d\n",e, f);
return 0;
}


a = 11, b = 10
c = 11, d = 10
e = 11, f = 10
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: