求助一道C语言题...555~~

来源:百度知道 编辑:UC知道 时间:2024/06/11 23:32:52
下列给定程序中,函数fun()的功能是:实现两个整数的交换。例如给a和b分别输入12和20,输出结果为:a=20 b=12
请改正程序中的错误(不要改动main函数,不得增加程序行或删除程序行,也不得改变程序的结构),使它能得出正确的结果,并上机调试验证,最后提交正确的程序清单。

#include <stdio>
void fun(int a,b)
{int t;
t=b;
b=a;
a=b
}

main()
{int a,b;
printf(“Enter a,b:”);
scanf(“%d,%d”,a,b);
fun(a,b);
printf(“a=%d b=%d\n”,a,b);
}

注意不同之处
#include <stdio.h>
void fun(int *a,int *b)
{int t;
t=*b;
*b=*a;
*a=t;
}

main()
{int a,b;
printf(“Enter a,b:”);
scanf(“%d,%d”,&a,&b);
fun(&a,&b);
printf(“a=%d b=%d\n”,a,b);
}

#include <stdio.h>
void fun(int a,b)
{int t;
t=b;
b=a;
a=t
}

main()
{int a,b;
printf(“Enter a,b:”);
scanf(“%d,%d”,&a,&b);
fun(a,b);
printf(“a=%d b=%d\n”,a,b);
}
好久没做,忘了,不知道对不对

使用指针吧。
void fun(int *a, int *b)
{
int t;
t=*b;
*b=*a;
*a=t;
}

main()
{int a,b;
printf("Enter a,b:");
scanf("%d,%d",&a,&b);
fun(&a,&b);
printf("a=%d b=%d\n",a,b);
}

把函数头改为“void fun(int &c,&d)”函数中相应的“a”改“c”,“b”改“d”就可以了

#include <stdio>
void fun(int &a,&b)
{int t;
t=b;