当我尝试调用函数时,它会显示错误
我是编码的新手。我使用了一个简单的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
Sayhi();
return 0;
}
void Sayhi()
{
printf("hi");
}
因此,当我编译代码时,它在此范围中没有声明“ sayhi”函数。 我什至尝试了使用“ void”作为函数的其他代码,但它不起作用。
I am pretty new to coding. I used a simple code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
Sayhi();
return 0;
}
void Sayhi()
{
printf("hi");
}
So when I compile the code it says function "sayhi" was not declared in this scope.
I even tried a different code which used "void" as a function but it didn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该起作用 - 在使用它之前,请简单地声明并定义“ sayhi()”:
a“更好”方法就是为“ sayhi()”创建一个原型:
q:q:什么是“原型”的原型?
原型应始终列出函数的参数。如果没有参数,则应列出“ void”。
随着应用的大小和复杂性的增加,原型的价值会亮起。您需要将“ main()”的代码移出将其移至单独的.c源文件(例如“ mycomponent.c”)和相应的标头文件(例如“ myheader.h”)。
另一个注意:您应该始终 name 原型中的变量(例如
void myfunc(int i);
。q:您了解为什么要获得编译错误(在使用它之前需要以某种方式声明该功能),以及如何修复它?
This should work - simply declare and define "Sayhi()" before you use it:
A "better" approach would be to create a prototype for "Sayhi()":
Q: So what's a "prototype"?
Prototypes should always list the function's parameters. If no parameters, it should list "void".
The value of prototypes shines as your application increases in size and complexity. You'll want to move code OUT of "main()" and into separate .c source files (e.g. "mycomponent.c") and corresponding header files (e.g. "myheader.h").
One additional note: you should always NAME the variables in your prototypes (e.g.
void myfunc(int i);
.Q: Do you understand why you were getting the compile error (the function needed to be declared somehow before you used it), and how you can fix it?