麻烦编译斐波纳契程序 [英] trouble compiling fibonacci program
问题描述
大家好,
我在90年代早期摆弄BASIC但是把它留在了
那个。现在我正在努力学习C.我试图在我的书中解决一个
练习,但它很难编译。可以
任何人都告诉我错误信息的含义&我应该怎么做?
谢谢。
#include< stdio.h>
长柜台;
无效主要(无效)
{
长纤维;
double fibn_value;
printf(" Enter fibonacci number:");
scanf("%ld",& fibn);
fibn_value = calc_fib(fibn);
printf(" \ nThe%ld fibonacci number is%lf \ n",fibn,
fibn_value );
返回;
}
双calc_fib(长纤维)
{
double fibn_value = 1;
double previous1 = 1;
double previous2 = 0;
if(fibn == 1)return(double)0;
else if(fibn == 2)return(double)1;
for(counter = 3; counter< = fibn;计数器++){
fibn_value = previous1 + previous2;
previous2 = previous1;
previous1 = fibn_value;
}
返回fibn_value;
}
#gcc fib00.c
fib00.c:在函数''main'':
fib00.c:6:警告:返回类型'' main''不是''int''
fib00.c:顶层:
fib00.c:17:错误:calc_fib的冲突类型
fib00.c:11:错误:先前的隐含声明
''calc_fib''在这里
#
-
电子邮件:删除z'并反转其余部分。
hello all,
I fiddled with BASIC in the early 90s but left it at
that. Now I am trying to learn C. I tried to solve an
exercise in my book, but it failes to compile. Can
anyone tell me what the error messages mean & what I
should do?
thanks.
#include <stdio.h>
long counter;
void main(void)
{
long fibn;
double fibn_value;
printf("Enter fibonacci number: ");
scanf("%ld", &fibn);
fibn_value = calc_fib(fibn);
printf("\nThe %ld fibonacci number is %lf\n", fibn,
fibn_value);
return;
}
double calc_fib(long fibn)
{
double fibn_value = 1;
double previous1 = 1;
double previous2 = 0;
if(fibn == 1) return (double) 0;
else if(fibn == 2) return (double)1;
for(counter = 3; counter <= fibn; counter++) {
fibn_value = previous1 + previous2;
previous2 = previous1;
previous1 = fibn_value;
}
return fibn_value;
}
# gcc fib00.c
fib00.c: In function ''main'':
fib00.c:6: warning: return type of ''main'' is not ''int''
fib00.c: At top level:
fib00.c:17: error: conflicting types for ''calc_fib''
fib00.c:11: error: previous implicit declaration
of ''calc_fib'' was here
#
--
email: remove z''s and reverse the rest.
推荐答案
Santosh Krisnan写道:
Santosh Krisnan wrote:
你好,
我在90年代早期摆弄BASIC但是留在了
那个。现在我正在努力学习C.我试图在我的书中解决一个
练习,但它很难编译。可以
任何人都告诉我错误信息的含义&我应该怎么做?
#gcc fib00.c
fib00.c:在函数''main''中:
fib00.c:6:警告:返回类型''main''不是''int''
hello all,
I fiddled with BASIC in the early 90s but left it at
that. Now I am trying to learn C. I tried to solve an
exercise in my book, but it failes to compile. Can
anyone tell me what the error messages mean & what I
should do?
# gcc fib00.c
fib00.c: In function ''main'':
fib00.c:6: warning: return type of ''main'' is not ''int''
int main( void)
int main(void)
fib00.c:顶级:
fib00.c:17:错误:''calc_fib''的冲突类型
fib00.c:11:错误:先前的隐含声明
''calc_fib''在这里
fib00.c: At top level:
fib00.c:17: error: conflicting types for ''calc_fib''
fib00.c:11: error: previous implicit declaration
of ''calc_fib'' was here
在main之前没有calc_fib的原型,所以编译器
假定函数返回int。要么提供原型,要么交换
main和calc_fib。
-
Ian Collins。
There isn''t a prototype for calc_fib before main, so the compiler
assumes the function returns int. Either provide a prototype, or swap
main and calc_fib.
--
Ian Collins.
Santosh Krisnan写道:
Santosh Krisnan wrote:
hello all,
我在BASIC中摆弄90年代初,但它留在了
那个。现在我正在努力学习C.我试图在我的书中解决一个
练习,但它很难编译。可以
任何人都告诉我错误信息的含义&我应该怎么做?
谢谢。
#include< stdio.h>
长柜台;
hello all,
I fiddled with BASIC in the early 90s but left it at
that. Now I am trying to learn C. I tried to solve an
exercise in my book, but it failes to compile. Can
anyone tell me what the error messages mean & what I
should do?
thanks.
#include <stdio.h>
long counter;
使用静态局部变量而不是全局变量。它减少了程序其他部分的无意中的修改(对于这样一个简单的例子来说不是必需的,但是当你的程序得到它时它会变得很有用>
更大)。
Use a static local variable instead of a global. It reduces inadvertant
modifications by other parts of the program, (not necessary for such a
trivial example, but it''ll become useful when your programs get
bigger).
void main(void)
void main(void)
标准C定义main( )作为int main(void)或int main(int
argc,char ** argv)。任何其他签名都不可移植。当然你
可以使用你自己的名字而不是argc和argv,但它们是规范的。
另外** argv可以写成* argv []。
Standard C defines main() as either int main(void) or int main(int
argc, char **argv). Any other signature is not portable. Of course you
can use your own names instead of argc and argv, but they''re canonical.
Also **argv can be written as *argv[].
{
long fibn;
double fibn_value;
printf(" ;输入斐波纳契数:);
{
long fibn;
double fibn_value;
printf("Enter fibonacci number: ");
除非输出被换行终止,否则不能保证在stdout(通常是屏幕)上立即显示
。或者拨打
fflush(stdout)。
Unless output is terminated by a newline it''s not guaranteed to appear
on stdout, (typically the screen), immediatly. Alternatively call
fflush(stdout).
scanf("%ld",& fibn);
fibn_value = calc_fib(fibn);
printf(" \ n%ld斐波纳契数是%lf \ n",fibn,
fibn_value);
scanf("%ld", &fibn);
fibn_value = calc_fib(fibn);
printf("\nThe %ld fibonacci number is %lf\n", fibn,
fibn_value);
不要将%lf用于双打。这是非标准的。使用%f。
Don''t use %lf for doubles. It''s non-standard. Use %f.
return;
return;
并在这里返回一个int。
And return an int here.
}
double calc_fib(long fibn)
{
double fibn_value = 1;
double previous1 = 1;
double previous2 = 0;
if(fibn == 1)return(double)0;
else if(fibn == 2)return(double)1;
for(counter = 3; counter< = fibn; counter ++){
fibn_value = previous1 + previous2;
previous2 = previous1;
previous1 = fibn_value;
}
返回fibn_value;
}
#gcc fib00.c
fib00.c:在函数''main'':
fib00.c:6:警告:返回类型''main''是不是''int''
fib00.c:顶级:
fib00.c:17:错误:''calc_fib'的冲突类型'
fib00.c:11:错误:先前的隐含声明
''cal c_fib''在这里
#
}
double calc_fib(long fibn)
{
double fibn_value = 1;
double previous1 = 1;
double previous2 = 0;
if(fibn == 1) return (double) 0;
else if(fibn == 2) return (double)1;
for(counter = 3; counter <= fibn; counter++) {
fibn_value = previous1 + previous2;
previous2 = previous1;
previous1 = fibn_value;
}
return fibn_value;
}
# gcc fib00.c
fib00.c: In function ''main'':
fib00.c:6: warning: return type of ''main'' is not ''int''
fib00.c: At top level:
fib00.c:17: error: conflicting types for ''calc_fib''
fib00.c:11: error: previous implicit declaration
of ''calc_fib'' was here
#
编译器需要找到一个函数的原型,如果它被称为
在定义之前
。在这里你可以在main()中调用calc_fib(),但它只是稍后定义的
,因此编译器假定默认返回int,
与它冲突'定义。
将原型放在任何函数定义之前,就在
标题包含之后。
The compiler needs to find the prototype of a function if it''s called
before it''s definition. Here you call calc_fib() in main() but it''s
defined only later, hence the compiler assumes a default return of int,
which conflicts with it''s definition.
Place the prototype before any function definitions, right after the
header includes.
santosh说:
santosh said:
Santosh Krisnan写道:
Santosh Krisnan wrote:
< snip>
<snip>
>>
#include< stdio.h>
long counter;
>>
#include <stdio.h>
long counter;
使用静态局部变量而不是全局变量。
Use a static local variable instead of a global.
再看一遍。它不需要是静态的。
< snip>
Look again. It doesn''t need to be static.
<snip>
>
>
> {
long fibn;
double fibn_value;
printf(" Enter fibonacci number:");
>{
long fibn;
double fibn_value;
printf("Enter fibonacci number: ");
除非输出被换行终止,否则不能保证在stdout(通常是屏幕)上立即出现
。或者打电话给
fflush(标准输出)。
Unless output is terminated by a newline it''s not guaranteed to appear
on stdout, (typically the screen), immediatly. Alternatively call
fflush(stdout).
正确。
Right.
>
>
> scanf("%ld",& fibn);
> scanf("%ld", &fibn);
到OP:检查返回值。 scanf可能会失败。
To the OP: check the return value. scanf can fail.
> fibn_value = calc_fib(fibn);
printf(" \\\
%ld fibonacci number is%lf \ n",fibn,
fibn_value);
> fibn_value = calc_fib(fibn);
printf("\nThe %ld fibonacci number is %lf\n", fibn,
fibn_value);
不要将%lf用于双打。这是非标准的。使用%f。
Don''t use %lf for doubles. It''s non-standard. Use %f.
为了严格准确,它*现在是标准的,因为C99将其编码。然而,这是b $ b不可携带到C90,这就是世界和他的狗实际上使用的价格。
< snip>
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上述域名中, - www。
To be strictly accurate, it *is* now standard, since C99 codifies it. It is,
however, non-portable to C90, which is what the world and his dog actually
uses.
<snip>
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
这篇关于麻烦编译斐波纳契程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!