今天闲来无事,竟然写了个while(true),然而结果也非常让我吃惊。结果竟然编译错误。。下面是我的源代码:
#include<stdio.h>
int main(){
while ( true ){
break;
}
return 0;
}
采用gcc4.1.2编译,错误信息如下所示:
gcc test_true.c
test_true.c: In function 'main':
test_true.c:4: error: 'true' undeclared (first use in this function)
test_true.c:4: error: (Each undeclared identifier is reported only once
test_true.c:4: error: for each function it appears in.)
也就是说while循环把true当做了变量,而不是内置关键字。。
经过今天查询得知:和我采用的gcc编译器有关。gcc不支持,但是g++支持。这是因为true并非c99标准关键字。c99中的bool类型采用关键字_Bool声明,而且形如_Bool a = true;这种语句使用gcc不能编译通过的。但是如果要使用,使用g++进行编译即可。或者使用_Bool a = 1;这种形式。