C语言 使用未声明的标识符 'true'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13322709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of undeclared identifier 'true'
提问by gadgetmo
Why am I getting this error:
为什么我收到此错误:
infinite.c:5:12: error: use of undeclared identifier 'true'
while (true) {
1 error generated.
make: *** [infinite] Error 1
... when I try to compile this simple code for an infinite loop?
...当我尝试为无限循环编译这个简单的代码时?
#include <stdio.h>
int main(void) {
int x = 0;
while (true) {
printf("%i\n", x);
}
}
回答by md5
The identifier trueis not declared by default. To use it, two solutions :
true默认情况下不声明标识符。要使用它,有两种解决方案:
- Compile in C99 and include
<stdbool.h>. - Define this identifier by yourself.
- 在 C99 中编译并包含
<stdbool.h>. - 自己定义这个标识符。
However, the infinite loop for (;;)is often considered as better style.
然而,无限循环for (;;)通常被认为是更好的风格。
回答by Adam Sznajder
C has no built-in boolean types. So it doesn't know what trueis. You have to declare it on your own in this way:
C 没有内置的布尔类型。所以它不知道是什么true。您必须以这种方式自行声明:
#define TRUE 1
#define FALSE 0
[...]
while (TRUE) {
[...]
}
回答by Ramy Al Zuhouri
Include stdbool.h to use C99 booleans.
If you want to stick with C89 define it yourself:
包括 stdbool.h 以使用 C99 布尔值。
如果你想坚持使用 C89 自己定义它:
typedef enum
{
true=1, false=0
}bool;
回答by Batman
You are Getting this error because you have not defined the values of true and false in C. You can do that by adding few simple lines to your code as follows:
您之所以收到此错误,是因为您尚未在 C 中定义 true 和 false 的值。您可以通过在代码中添加几行简单的行来做到这一点,如下所示:
#define FALSE 0
#define TRUE 1 // Option 1
#define TRUE !FALSE // Option 2

