Xcode - 警告:C99 中隐式声明函数无效

104

收到警告:在C99中,函数“Fibonacci”的隐式声明无效。出了什么问题?

#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*/
4个回答

110

在调用函数之前必须先声明函数,可使用以下方法:

  • 在头文件中写下原型
    若该函数将从多个源文件中调用,请在 .h 文件(如 myfunctions.h)中编写原型
    int Fibonacci(int number);
    然后在 C 代码中加入 #include "myfunctions.h"

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

  • 在第一次调用函数之前显式声明函数
    这是上述两种方式的结合:在 C 文件中输入函数的原型,然后再编写 main() 函数。

另外提示:如果函数 int Fibonacci(int number) 只在实现它的文件中使用,则应将其声明为 static,以便它仅在该翻译单元中可见。


为什么我必须在头文件中输入 int Fibonacci(int number);?我认为 int Fibonacci(int); 应该没问题吧? - Ka Wa Yip

31
编译器需要在使用函数之前了解该函数,只需在调用函数之前声明即可。
#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)
{
//…

4
在C语言中,函数在调用之前必须进行声明。
包含头文件。

1

我遇到了同样的警告(导致我的应用无法构建)。当我在Objective-C的.m文件中添加C函数时,但忘记在.h文件中声明它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接