首先要说明的是,C语言是没有bool这种类型的(C++是有的)
C99标准添加了_Bool类型,用于表示布尔值,即逻辑值true和false
#include <stdio.h>
int main()
{
_Bool flag = 1;
if (flag) {
printf("true");
} else {
printf("false");
}
return 0;
}
输出结果为true
当然如果觉得_Bool这个类型比较碍眼,希望能够像C++那样使用bool类型
可以通过include头文件stdbool.h来实现
#include <stdio.h>
#include <stdbool.h>
int main()
{
bool flag = true;
if (flag) {
printf("true");
} else {
printf("false");
}
return 0;
}