【发布时间】:2014-10-21 05:55:52
【问题描述】:
以下代码在 unix 机器上使用 gcc 编译时抛出以下错误:
eu.c: In function 'main':
eu.c:23: error: 'valuetoAdd' undeclared (first use in this function)
eu.c:23: error: (Each undeclared identifier is reported only once
eu.c:23: error: for each function it appears in.)
这是代码:我已经声明了变量 valueToAdd。为什么它不起作用?
#include <stdio.h>
int main (void)
{
float input;
float e = 0;
int count = 0;
int flag = 1;
float valueToAdd = 0.0;
do
{
printf("Enter input: ");
scanf("%f", &input);
} while (input < 0);
while (flag)
{
valueToAdd = 1 / factorial(count);
e += valuetoAdd;
count++;
flag = input < valueToAdd;
}
printf("The computed value of e is: %.15f", e);
printf("%d terms were required", count++);
}
int factorial (int c)
{
if (c == 0) return 1;
else return c * factorial(c-1);
}
【问题讨论】:
-
你要么需要在 main 之上的原型,要么你需要在 main 之上声明它。 main 也应该返回一个 int。
-
我做了它仍然抛出同样的错误。