gpt4 book ai didi

c - 这个C程序有什么错误?

转载 作者:行者123 更新时间:2023-11-30 18:31:28 26 4
gpt4 key购买 nike

我做了两个简单的程序,但得到了意想不到的答案。有人可以帮忙吗?

//main program
#include<stdio.h>
#include<conio.h>
float main(int argc, char* argv[])
{
float a,b,c;
a=5; b=10;
c=sum(a,b);
printf("%f + %f = %f \n",a,b,c);
return (0);
}

//sum program
#include<stdio.h>
float sum (float a,float b)
{
float c;
c=a+b;
return(c);
}

该程序的输出出现错误。我不明白为什么。我用gcc编译程序。有人可以评论吗?

最佳答案

这应该是您的实际代码:

#include<stdio.h>

float sum (float,float);
int main(int argc, char* argv[])
{
float a,b,c;
a=5; b=10;
c=sum(a,b);
printf("%f + %f = %f \n",a,b,c);
return (0);
}

//sum program
float sum (float a,float b)
{
float c;
c=a+b;
return(c);
}

最初,您没有提到 sum 的原型(prototype),通常根据 C99,它应该默认为 int,因此您的代码将无法编译 - 并且您还包含了 stdio.h 两次,并且您无缘无故地包含了 conio.h 。另外,根据 ISO,main 应返回 int 而不是 float,并且 conio.h 是非标准的。

关于c - 这个C程序有什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22989861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com