【问题标题】:"Undeclared: first use in function error" in while loop despite declaring [closed]尽管声明了[关闭],但在while循环中“未声明:首次在函数中使用错误”
【发布时间】: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。
  • 我做了它仍然抛出同样的错误。

标签: c gcc


【解决方案1】:

应该是valueToAdd,您使用的是valuetoAdd ..检查大小写!!

【讨论】:

  • 哈哈尴尬的错误。谢谢!
【解决方案2】:

其实你没有声明valuetoAdd。您声明了valueToAdd,但这是一个不同的标识符。 C 中的标识符区分大小写。

【讨论】:

    【解决方案3】:
    valueToAdd = 1 / factorial(count);
    e += valuetoAdd;  <= Problem is here, valuetoAdd undeclared !
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 2018-10-03
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多