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

Cod*_*eam 67 c xcode

获得警告: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*/
Run Code Online (Sandbox Code Playgroud)

eck*_*kes 74

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

  • 在标题中记下原型
    如果函数可以从多个源文件中调用,请使用此选项.只要写你的原型
    int Fibonacci(int number);
    下来的.h文件(例如myfunctions.h),然后#include "myfunctions.h"在C代码.

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

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

作为补充说明:如果该函数int Fibonacci(int number)仅用于其实现的文件中,则应声明该函数static,以便它仅在该转换单元中可见.

  • 这看起来太小了,不能保证一个新问题,所以我在这里问. (4认同)

Vik*_*ica 24

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

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

#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)
{
//…
Run Code Online (Sandbox Code Playgroud)