C语言 “隐式函数声明”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2161304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:19:14  来源:igfitidea点击:

What does "implicit declaration of function" mean?

c

提问by bob

#include <stdio.h>

int main()
{
    int a = 4;
    int b = 3;
    addNumbers(a, b);
}

int addNumbers(int a, int b)
{
    return a + b;
}

Why does this not compile, I get a message saying implicit declaration of function addNumbers()?

为什么这不能编译,我收到一条消息,说隐式声明函数addNumbers()

回答by Prasoon Saurav

Either define the function before main()or provide its prototype before main().

在之前定义函数main()或在之前提供其原型main()

So either do this:

所以要么这样做:

#include <stdio.h>

int addNumbers(int a, int b)
{ //definition
}

int main()
{ //Code in main
  addNumbers(a, b);
}

or this:

或这个:

#include <stdio.h>

int addNumbers(int, int);
int main()
{ //Code in main
  addNumbers(a, b);
}

int addNumbers(int a, int b)
{ // definition
}

回答by rui

You need to declare the function before you call it in main(). Either move it before main or at least declare it there. Also, you should prob add return 0at the end of the main function since it's supposed to return int.

您需要在 main() 中调用它之前声明该函数。要么在 main 之前移动它,要么至少在那里声明它。此外,您应该 prob addreturn 0在 main 函数的末尾,因为它应该返回int.

#include <stdio.h>

int addNumbers(int a, int b)
{
    return a + b;
}

int main()
{
    int a = 4;
    int b = 3;
    addNumbers(a, b);
    return 0;
}

回答by sharptooth

You have to either move the entire addNumber()function above main or provide a prototype. The latter is done the following way:

您必须将整个addNumber()函数移到main 之上或提供一个原型。后者通过以下方式完成:

int addNumbers(int a, int b);

int main()
{
//    code of main() here
}

int addNumbers(int a, int b)
{
//code of addNumbers() here
}

回答by YOU

Put addNumbers before main

将 addNumbers 放在 main 之前

int addNumbers(int a, int b)
{
    return a + b;
}

int main()
{
    int a = 4;
    int b = 3;

    addNumbers(a, b);
}

UPDATE:

更新

To print it, printf("%i",addNumbers(a, b));will display 7in above case.

要打印它,printf("%i",addNumbers(a, b));7在上述情况下显示。

回答by sergiom

You can move the whole function above the point where it is called, or use a function prototype, like this:

您可以将整个函数移动到调用点的上方,或者使用函数原型,如下所示:

#include <stdio.h>

int addNumbers(int a, int b); // function prototype

int main()
{
    int a = 4;
    int b = 3;
    addNumbers(a, b);
}

int addNumbers(int a, int b)
{
    return a + b;
}

回答by miku

You'll need a forward declaration of the addNumbersfunction or its definition moved up before the first usage:

addNumbers在第一次使用之前,您需要对函数或其定义进行前向声明:

// 2161304
#include <stdio.h>

// forward declaration
int addNumbers(int a, int b);

int main()
{
    int a = 4;
    int b = 3;
    addNumbers(a, b);
}

// alternatively move this up before main ...
int addNumbers(int a, int b)
{
    return a + b;
}

Regarding mainand return:

关于mainreturn

Programs will compile without. The signatures of main defined by the standard are:

程序将编译没有。标准定义的 main 的签名是:

int main(void)
int main(int argc, char **argv)

There are three portable return values:

有三个可移植的返回值:

0, EXIT_SUCCESS, EXIT_FAILURE

which are defined in stdlib.h.

中定义的stdlib.h

回答by Mark Tolonen

Declare the function before using it by either adding a prototype before main():

在使用它之前通过在 main() 之前添加原型来声明该函数:

int addNumbers(int a, int b);

or move the whole addNumbers function before main().

或者在 main() 之前移动整个 addNumbers 函数。

回答by Srinivas Reddy Thatiparthy

I agree with declaration and definition thing but i am not getting any compilation errors with the above code.My gcc version is "4.4.1-4ubuntu9".Any ideas.

我同意声明和定义的事情,但上面的代码没有出现任何编译错误。我的 gcc 版本是“4.4.1-4ubuntu9”。任何想法。

I have done a little modification to test the code.

我做了一些修改来测试代码。

 #include <stdio.h>

int main()
{
    int a = 4;
    int b = 3;
    printf("%d", addNumbers(a, b));   // Printf added.
}

int addNumbers(int a, int b)
{
    return a + b;
}

回答by Chankey Pathak

You must declare the function before using. Remember these 4 basic parts while dealing with functions.

您必须在使用前声明该函数。在处理函数时记住这 4 个基本部分。

  1. Declaration
  2. Call
  3. Definition
  4. Return
  1. 宣言
  2. 称呼
  3. 定义
  4. 返回

回答by C Learner

if your compiler is C99 standard it throws and error "implicit declaration", since since default promotion is obsolete in C99 standard. if you try to compile with C89 standard this would be allowable.

如果您的编译器是 C99 标准,它会抛出错误“隐式声明”,因为默认升级在 C99 标准中已过时。如果您尝试使用 C89 标准进行编译,这将是允许的。

In C99 standard function prototype is necessary

在 C99 标准函数原型中是必要的