首页 > 编程语言 > 详细

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

时间:2021-06-15 10:00:31      阅读:21      评论:0      收藏:0      [点我收藏+]
#include <stdio.h>
int main()
{
    int a,b;
    //方法一:借助第三个变量 
    int t;
    a=1,b=2;
    t=a;
    a=b;
    b=t;
    printf("%d,%d\n",a,b); 
    //方法二 :先保存两数之和 
    a=1,b=2;
    a=a+b;
    b=a-b;
    a=a-b;
    printf("%d,%d\n",a,b);
    //方法三 :先保存两数积 (不能有0) 
    a=1,b=2;
    a=a*b;
    b=a/b;
    a=a/b;
    printf("%d,%d\n",a,b);
    //方法四 :利用异或 , 
    a=1,b=2;
    a=a^b;
    b=a^b;
    a=a^b;
    printf("%d,%d\n",a,b);
    getchar();
}

 

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

原文:https://www.cnblogs.com/xkdn/p/14884053.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!