C语言交换两个数的值

#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

 

posted @ 2014-01-21 10:40  天之涯0204  阅读(307)  评论(0编辑  收藏  举报