C语言交换2个数的值

[复制链接]
查看11 | 回复3 | 2018-4-13 14:54:43 | 显示全部楼层 |阅读模式
你所说的使用指针一般是用在子函数里进行比较的,因为一个函数只能返回一个值,在函数中值互换后,需要返回两个值,所以,有人就想到了使用指针,因为指针指向的是地址,内存单元里面的内容互换后;a,b的值也会随之改变,这样,就可以不用返回值便能达到效果, 不使用临时变量可很简单,就是上面写#include main(){int M,N; printf("input valuefor M and N:\n"); scanf("%d %d\n",&M,&N); M=M+N; //把两数之和放在M中 N=M-N;//两数之和 减去 N的值,即M的值放在N中
M=M-N; //两数之和 减去 M的值(现...
回复

使用道具 举报

千问 | 2018-4-13 14:54:43 | 显示全部楼层
不要临时变量的写法#include int main(){
int a ,b;
printf("请输入a 和 b 的值:\n");
scanf("%d%d",&a,&b) ;
printf(" a 的值为%d\n",a);
printf(" b 的值为%d\n",b);
a =...
回复

使用道具 举报

千问 | 2018-4-13 14:54:43 | 显示全部楼层
#includevoid main(){int a,b,c; printf("Please get in your date:\n"); scanf("%d %d\n",&a,%b); c=a;a=b;b=c; printf("Now the date a and b are :“a,b);}...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行