i wrote this c code in sublime text 2
我在崇高的文本2中写了这个c代码
#include <stdio.h>
int main()
{
int n, m;
scanf("%d", &n);
m = fib(n);
printf("%d", m);
return 0;
}
int fib(int n)
{
if(n == 0)
return 0;
else if( n == 1)
return 1;
else
return fib(n - 1) + fib( n - 2);
}
but i when i build it, the console comes following fault:
但是当我构建它时,控制台出现以下错误:
/home/shieh/program.c: In function ‘int main()’:
/home/shieh/program.c:6:14: error: ‘fib’ was not declared in this scope
m = fib(n);
^
[Finished in 0.0s with exit code 1]
however, this c code could be accepted by an online testing platform.Anyone could help me fix this problem? I am confused now.
但是,这个c代码可以被在线测试平台接受。任何人都可以帮我解决这个问题吗?我现在很困惑。
1 个解决方案
#1
2
You have to declare
your function's before
you call it! Otherwise the function call in main
can't be done, since at compilation time
the function isn't known!(The compiler goes from the top to the bottom!) So try this:
在打电话之前你必须申报你的功能!否则无法完成main中的函数调用,因为在编译时函数未知!(编译器从上到下!)试试这个:
#include <stdio.h>
int fib(int n) {
if(n == 0)
return 0;
else if( n == 1)
return 1;
else
return fib(n - 1) + fib( n - 2);
}
int main() {
int n, m;
scanf("%d", &n);
m = fib(n);
printf("%d", m);
return 0;
}
OR you make a function prototype
like this before main
:
或者你在main之前制作一个这样的函数原型:
#include <stdio.h>
int fib(int n);
int main() {
int n, m;
scanf("%d", &n);
m = fib(n);
printf("%d", m);
return 0;
}
int fib(int n) {
if(n == 0)
return 0;
else if( n == 1)
return 1;
else
return fib(n - 1) + fib( n - 2);
}
(I prefer the variante with the prototype so that your main is always at the top of your file and you see with the prototype which function's this file includes!)
(我更喜欢带有原型的variante,以便你的main始终位于文件的顶部,你可以看到原型该文件包含哪个函数!)
#1
2
You have to declare
your function's before
you call it! Otherwise the function call in main
can't be done, since at compilation time
the function isn't known!(The compiler goes from the top to the bottom!) So try this:
在打电话之前你必须申报你的功能!否则无法完成main中的函数调用,因为在编译时函数未知!(编译器从上到下!)试试这个:
#include <stdio.h>
int fib(int n) {
if(n == 0)
return 0;
else if( n == 1)
return 1;
else
return fib(n - 1) + fib( n - 2);
}
int main() {
int n, m;
scanf("%d", &n);
m = fib(n);
printf("%d", m);
return 0;
}
OR you make a function prototype
like this before main
:
或者你在main之前制作一个这样的函数原型:
#include <stdio.h>
int fib(int n);
int main() {
int n, m;
scanf("%d", &n);
m = fib(n);
printf("%d", m);
return 0;
}
int fib(int n) {
if(n == 0)
return 0;
else if( n == 1)
return 1;
else
return fib(n - 1) + fib( n - 2);
}
(I prefer the variante with the prototype so that your main is always at the top of your file and you see with the prototype which function's this file includes!)
(我更喜欢带有原型的variante,以便你的main始终位于文件的顶部,你可以看到原型该文件包含哪个函数!)