C语言 Xcode - 警告:函数的隐式声明在 C99 中无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15850042/
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 05:59:00  来源:igfitidea点击:

Xcode - Warning: Implicit declaration of function is invalid in C99

cxcode

提问by CodingUpStream

Getting a warning : Implicit declaration of function 'Fibonacci' is invalid in C99. What's wrong?

收到警告:函数 'Fibonacci' 的隐式声明在 C99 中无效。怎么了?

#include <stdio.h>

int main(int argc, const char * argv[])
{
    int input;
    printf("Please give me a number : ");
    scanf("%d", &input);
    getchar();
    printf("The fibonacci number of %d is : %d", input, Fibonacci(input)); //!!!

}/* main */

int Fibonacci(int number)
{
    if(number<=1){
        return number;
    }else{
        int F = 0;
        int VV = 0;
        int V = 1;
        for (int I=2; I<=getal; I++) {
            F = VV+V;
            VV = V;
            V = F;
        }
        return F;
    }
}/*Fibonacci*/

回答by eckes

The function has to be declared before it's getting called. This could be done in various ways:

该函数必须在被调用之前声明。这可以通过多种方式完成:

  • Write down the prototype in a header
    Use this if the function shall be callable from several source files. Just write your prototype
    int Fibonacci(int number);
    down in a .hfile (e.g. myfunctions.h) and then #include "myfunctions.h"in the C code.

  • Move the function before it's getting called the first time
    This means, write down the function
    int Fibonacci(int number){..}
    before your main()function

  • Explicitly declare the function before it's getting called the first time
    This is the combination of the above flavors: type the prototype of the function in the C file before your main()function

  • 在头文件中写下原型
    如果该函数可以从多个源文件中调用,请使用它。只需将您的原型写
    int Fibonacci(int number);
    在一个.h文件中(例如myfunctions.h),然后#include "myfunctions.h"在 C 代码中。

  • 在第一次调用之前移动函数
    这意味着,
    int Fibonacci(int number){..}
    在你的函数之前写下main()函数

  • 显式声明的功能,它变得首次调用之前
    这是上面口味的组合:您之前键入C文件的函数的原型main()功能

As an additional note: if the function int Fibonacci(int number)shall only be used in the file where it's implemented, it shall be declared static, so that it's only visible in that translation unit.

作为附加说明:如果该函数int Fibonacci(int number)只能在实现它的文件中使用,则应声明static它,以便它仅在该翻译单元中可见。

回答by vikingosegundo

The compiler wants to know the function before it can use it

编译器想在使用函数之前知道它

just declare the function before you call it

只需在调用之前声明该函数

#include <stdio.h>

int Fibonacci(int number); //now the compiler knows, what the signature looks like. this is all it needs for now

int main(int argc, const char * argv[])
{
    int input;
    printf("Please give me a number : ");
    scanf("%d", &input);
    getchar();
    printf("The fibonacci number of %d is : %d", input, Fibonacci(input)); //!!!

}/* main */

int Fibonacci(int number)
{
//…

回答by lee

I have the same warning (it's make my app cannot build). When I add C functionin Objective-C's .m file, But forgot to declared it at .hfile.

我有同样的警告(它使我的应用程序无法构建)。当我添加C functionObjective-C's .m file,但忘记在.h文件中声明它。

回答by QuaziRidwanHasib

should call the function properly; like- Fibonacci:input

应该正确调用函数;喜欢-斐波那契:输入