Xcode - 警告:函数的隐式声明在 C99 中无效
收到警告:函数 'Fibonacci' 的隐式声明在 C99 中无效.怎么了?
Getting a warning : Implicit declaration of function 'Fibonacci' is invalid in C99. What's wrong?
#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*/
该函数必须在被调用之前声明.这可以通过多种方式完成:
The function has to be declared before it's getting called. This could be done in various ways:
在标题中写下原型
如果该函数可从多个源文件中调用,请使用此选项.只需编写您的原型int Fibonacci(int number);
在.h
文件中(例如myfunctions.h
),然后在 C 代码中#include "myfunctions.h"
.
Write down the prototype in a header
Use this if the function shall be callable from several source files. Just write your prototypeint Fibonacci(int number);
down in a.h
file (e.g.myfunctions.h
) and then#include "myfunctions.h"
in the C code.
在函数第一次被调用之前移动它
这意味着,写下函数int Fibonacci(int number){..}
在你的 main()
函数之前
Move the function before it's getting called the first time
This means, write down the functionint Fibonacci(int number){..}
before your main()
function
在第一次调用之前显式声明函数
这是上述风格的组合:在您的 main()
函数
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)
只在实现它的文件中使用,它应该被声明为 static
,这样它仅在该翻译单元中可见.
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.