C语言:交换两个变量的值

#include <stdio.h>
int main()
{
    int a=1,b=2;
    //方法一:借助第三个变量
    int t;
    t=a,a=b,b=t;
    printf("%d,%d",a,b);
    //方法2:利用变量的和与差
    a=1,b=2;
    a=a+b,b=a-b,a=a-b;
    printf("\n%d,%d",a,b);
    getchar();
}

 

posted @ 2021-06-07 14:47  myrj  阅读(240)  评论(0编辑  收藏  举报