gpt4 book ai didi

c - 函数声明中的参数名称(无类型)

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:55 25 4
gpt4 key购买 nike

所以,我正在做作业,我的老师没有很好地解释函数。我会节省一些时间并显示发生错误的主要部分:

 #include <stdio.h>
6 int main(void)
7 {
8 double Depth;
9 double celsius_at_depth(Depth);
10 {
11 10*(Depth)+20;
12 }

错误:

GHP#4.c:9:2: warning: parameter names (without types) 
in function declaration [enabled by default]
double celsius_at_depth(Depth);
^

抱歉关于格式,我想让它更容易看。 double 不应该是函数 celsius_at_depth 的参数类型吗?

编辑:我已经在网站上查找了错误,但我没有看到它与我的代码格式相同,所以我觉得最好重新发布

最佳答案

在另一个函数中定义一个函数是 gcc AFAIK 的非标准扩展,因此这不是一个好主意。

要声明函数,您首先需要将它移到main() 之外

double celsius_at_depth(double depth);

然后你可以像这样在main中调用它

#include <stdio.h>
int main(void)
{
double depth = 10;
printf("celsius_at_depth(%f) = %f\n", depth, celsius_at_depth(depth))
return 0;
}

然后是函数定义

double celsius_at_depth(double depth);
{
return 10 * depth + 20;
}

关于c - 函数声明中的参数名称(无类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29269227/

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