【问题标题】:Xcode - Warning: Implicit declaration of function is invalid in C99Xcode - 警告:函数的隐式声明在 C99 中无效
【发布时间】:2023-03-17 10:00:02
【问题描述】:

收到警告:函数“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*/

【问题讨论】:

    标签: c xcode


    【解决方案1】:

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

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

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

    • 在第一次调用之前显式声明函数
      这是上述风格的组合:在 C 文件中在 main() 函数之前键入函数的原型

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

    【讨论】:

    • @Cupidvogel:如果您有新问题,请提出新问题。
    • 这看起来太小了,无法提出新问题,所以我在这里问了。
    • 为什么我必须在标题中输入int Fibonacci(int number);?我认为int Fibonacci(int); 应该没问题?
    • @JavierVazquez,此页面上没有 Objective-C。
    • @vikingosegundo 我想我评论了另一条评论,但也许它被删除了......所以我也删除了我的评论;)
    【解决方案2】:

    编译器想知道函数才能使用它

    在调用之前声明函数

    #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)
    {
    //…
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        在 c 中 函数必须在被调用之前声明

        包含头文件

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-12-07
          • 1970-01-01
          • 2013-05-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多