2013-04-06 80 views
53

獲取警告:函數'Fibonacci'的隱式聲明在C99中無效。 有什麼問題?Xcode - 警告:隱式函數聲明在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*/ 

回答

57

功能都將被聲明它變得調用之前。這可以通過各種方式來完成:

  • 寫下原型在頭
    使用這個命令的作用,是從幾個源文件調用。只需在C代碼中寫下您的原型
    int Fibonacci(int number);
    向下.h文件(例如myfunctions.h),然後#include "myfunctions.h"

  • 移動功能,它越來越被稱爲第一次
    這意味着前,寫下功能
    int Fibonacci(int number){..}
    main()功能之前

  • 顯式聲明函數它越來越被稱爲第一次
    前 這是上述風味的組合:在C文件中輸入函數原型之前,你的main()函數

作爲附加說明:如果函數int Fibonacci(int number)只能用於其實現的文件中,則應將其聲明爲static,以便它僅在該翻譯單元中可見。

+1

@Cupidvogel:如果您有新問題,請提出新問題。 – eckes 2014-06-02 08:27:54

+4

這看起來太小了,不能提出一個新的問題,所以我在這裏問。 – SexyBeast 2014-06-02 11:39:36

+0

爲什麼我必須在頭中輸入'int Fibonacci(int number);'?我認爲'int斐波那契(int);'應該沒問題? – kyle 2017-03-22 18:57:42

22

編譯器想知道功能纔可以使用它

只是聲明函數調用之前

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

很多thx!有一些C#的經驗,它並不完全相同。 – CodingUpStream 2013-04-06 11:21:14

0

我有相同的警告(這使我的應用程序無法構建)。當我在Objective-C's .m file中添加C function時,忘記將其聲明爲.h文件。

相關問題