当我尝试调用函数时,它会显示错误

发布于 2025-02-12 16:57:47 字数 289 浏览 1 评论 0原文

我是编码的新手。我使用了一个简单的代码:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2025-02-19 16:57:47
  • 这应该起作用 - 在使用它之前,请简单地声明并定义“ sayhi()”:

      #include&lt; stdio.h&gt;
    #include&lt; stdlib.h&gt;
    
    void sayhi();
    { 
       printf(“ hi”);
    }
    
    int main()
    {
       sayhi();
       返回0;
    }
     
  • a“更好”方法就是为“ sayhi()”创建一个原型:

      #include&lt; stdio.h&gt;
    #include&lt; stdlib.h&gt;
    
    void sayhi(void);
    
    int main()
    {
       sayhi();
       返回0;
    }
    
    void sayhi();
    { 
        printf(“ hi”);
    }
     

q:q:什么是“原型”的原型?

https://www.programiz.com/c-programiz.com/c-programiz.com/c-programiz.com/c-programiz.com /c-用户定义的功能

功能原型只是声明函数的声明
指定功能的名称,参数和返回类型。不是
包含功能主体。

功能原型提供了编译器的信息
功能以后可以在程序中使用。

原型应始终列出函数的参数。如果没有参数,则应列出“ 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:

    #include <stdio.h>
    #include <stdlib.h>
    
    void Sayhi();
    { 
       printf("hi");
    }
    
    int main()
    {
       Sayhi();
       return 0;
    }
    
  • A "better" approach would be to create a prototype for "Sayhi()":

    #include <stdio.h>
    #include <stdlib.h>
    
    void Sayhi(void);
    
    int main()
    {
       Sayhi();
       return 0;
    }
    
    void Sayhi();
    { 
        printf("hi");
    }
    

Q: So what's a "prototype"?

https://www.programiz.com/c-programming/c-user-defined-functions

A function prototype is simply the declaration of a function that
specifies function's name, parameters and return type. It doesn't
contain function body.

A function prototype gives information to the compiler that the
function may later be used in the program.

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文