【问题标题】:GCC Error Infinite While LoopGCC 错误无限循环
【发布时间】:2015-11-23 02:42:35
【问题描述】:
#include <stdio.h>

int number, total;

int main (void)
{
    total = 0;

    while (true) {
        printf ("Enter a number (-1 to stop)\n");
        scanf (" %d", &number);
        if (number < 0) {
            break;
        }

        total = number + total;
    }

    printf ("The total is %d", total);

    return 0;
}

当我在 OSX 上的 C 程序中使用 while (true)loop 时,出现以下错误

'error': use of undeclared identifier 'true' while (true)

但是,当我在我的 Windows 分区上运行它时(通过 GCC 程序),我没有收到任何错误..

这是 GCC 对 osx 的限制吗?

【问题讨论】:

  • 你#include 有什么事吗?
  • 是的,这只是整个程序的 sn-p..
  • true 不是 C 关键字。您需要自己定义或包含定义它的标题(如stdbool.h)。甚至 gcc 也应该在该代码上失败 - 所以请显示完整的 MCVE 并提供您的 gcc 版本的确切详细信息,如果您发现其他情况。
  • Here is an example using gcc 无法按预期编译。不知道你是如何使用 gcc 成功编译的。

标签: c while-loop infinite-loop


【解决方案1】:

将以下内容添加到文件顶部:

#include <stdbool.h>

这将定义bool、true 和false。

【讨论】:

    猜你喜欢
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多