向c函数中传入参数时,参数值在函数内部发生了变化,不知道是什么原因,已经发了个贴,但还是不知道什么原因

zhjzh1016 2009-02-23 09:01:08
谢谢各位的耐心解答,但是我还是没明白,我重新贴下程序如下,我在输入时为“2,3,4”,屏幕上打印的结果为:
2,3,4
2.000000,3.000000,4.000000
0.000000,2.000000,0.000000
-63150641081726803000000000000000000.......请按任意键继续. . .
为什么传进去的参数值发生了变化??红色字体显示的

程序:
#include <stdio.h>
#include <string.h>
int main(){
float a,b,c;
scanf("%f,%f,%f",&a,&b,&c);
printf("%f,%f,%f",a,b,c);
printf("\n");
printf("%f",getTriangleArea(a,b,c));
return 0; /* Program termination. */
}
//求三角形的面积
float getTriangleArea(float x,float y ,float z)
{
float s=(x+y+z)/2;
float area=sqrt(s*(s-x)*(s-y)*(s-z));
printf("%f,%f,%f",x,y,z);
return area;
}
...全文
432 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Darkneece 2009-02-24
  • 打赏
  • 举报
回复
隐式的函数声明不会报错,但是会默认为返回值类型为int

所以math.h没包含也不会有编译、链接错,但是对于返回值不是int的sqrt等运行的时候就会有问题了。

而且C语言编译没有多次扫描,所以调用前必须先声明,不然会被当成默认的类型
waizqfor 2009-02-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhjzh1016 的回复:]
我用的vs2005,保存的名字为test.c,当时一直想不通,难道是和编译器有关系吗?晚上再回去试试
[/Quote]

#include <stdio.h>
#include <string.h>
#include <math.h>
float getTriangleArea(float x,float y ,float z) ;
int main(){
float a,b,c;
scanf("%f,%f,%f",&a,&b,&c);
printf("%f,%f,%f",a,b,c);
printf("\n");
printf("%f",getTriangleArea(a,b,c));
return 0; /* Program termination. */
}
//求三角形的面积
float getTriangleArea(float x,float y ,float z)
{
float s=(x+y+z)/2;
float area=sqrt(s*(s-x)*(s-y)*(s-z));
printf("%f,%f,%f",x,y,z);
return area;
}

这样应该没问题了 LZ试试 在我机器跑没问题
zhjzh1016 2009-02-23
  • 打赏
  • 举报
回复
我用的vs2005,保存的名字为test.c,当时一直想不通,难道是和编译器有关系吗?晚上再回去试试
zhjzh1016 2009-02-23
  • 打赏
  • 举报
回复
我用的vs2005,保存的名字为test.c,当时一直想不通,难道是和编译器有关系吗?
  • 打赏
  • 举报
回复
不要一次接收3个数据,一个一个接收,刷新缓冲区就可以了啊.
scanf("%f",&a);
scanf("%f",&b);
scanf("%f",&c);
我写成这样就没问题了.
chenzhp 2009-02-23
  • 打赏
  • 举报
回复
有这种事情?
sagegz 2009-02-23
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <string.h>
#include <math.h> //这个没有能用sqrt?

float getTriangleArea(float,float,float); //前置声明

int main(){
float a,b,c;
scanf("%f,%f,%f",&a,&b,&c);
printf("%f,%f,%f",a,b,c);
printf("\n");
printf("Area=%f",getTriangleArea(a,b,c));
return 0;
}

//求三角形的面积
float getTriangleArea(float x,float y ,float z){
float s=(x+y+z)/2;
float area=sqrt(s*(s-x)*(s-y)*(s-z));
printf("%f,%f,%f\n",x,y,z);
return area;
}

输入:2,3,4
输出:
2.000000,3.000000,4.000000
2.000000,3.000000,4.000000
Area=2.904737请按任意键继续. . .
沙漠里的海豚 2009-02-23
  • 打赏
  • 举报
回复

楼主你的开发环境是什么啊
我这里没问题啊

结果如下:

2,3,4
2.000000,3.000000,4.000000
2.000000,3.000000,4.0000002.904737
bitxinhai 2009-02-23
  • 打赏
  • 举报
回复
不知道你用的什么编译器,我用的vc6.0,运行是正常的!!!!

#include <stdio.h>
#include <string.h>
#include<math.h>

float getTriangleArea(float x,float y ,float z) ;
int main(){
float a,b,c;
scanf("%f,%f,%f",&a,&b,&c);
printf("%f,%f,%f",a,b,c);
printf("\n");
printf("%f",getTriangleArea(a,b,c));
return 0; /* Program termination. */
}
//求三角形的面积
float getTriangleArea(float x,float y ,float z)
{
float s=(x+y+z)/2;
float area=sqrt(s*(s-x)*(s-y)*(s-z));
printf("%f,%f,%f",x,y,z);
return area;
}
ldd909 2009-02-23
  • 打赏
  • 举报
回复
在main()函数执行前,应声明getTriangleArea函数
zhjzh1016 2009-02-23
  • 打赏
  • 举报
回复
我没有引用math.h,但是在编译是没有提示错误,当时我也觉得奇怪,晚上加上去看看有没有问题

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧