C89
)没有定义布尔类型,所以 C 语言判断真假时以 0
为假,非 0
为真C99
)提供了_Bool
型,_Bool
仍是整数类型,但与一般整型不同的是,_Bool
变量只能赋值为 0
或 1
,非 0
的值都会被存储为 1
,C99
还提供了一个头文件 <stdbool.h>
定义了 bool
代表_Bool
,true
代表 1
,false
代表 0
。只要导入 stdbool.h
,就能方便的操作布尔类型了 , 比如 bool flag = false;
if
#include<stdio.h>
void main(){
int isPass = -1;
if(isPass){// 0表示假,非0表示真
printf("通过考试");
}else{
printf("没有通过考试");
}
getchar();
}
#include<stdio.h>
#define BOOL int
#define TURE 1
#define FALSE 0
void main(){
//可以使用宏定义来完成
//定义一个布尔变量
BOOL isOK = TURE;//等价于 int isOK = 1
if(isOK){
printf("ok") ;
}
}