方法一:引入stdbool.h
#include <stdio.h>
#include <stdbool.h>
int main() {
bool f = false;
if (!f) {
printf("f is false\n");
}
return 0;
}
输出结果是f is false
相当于
#include <stdio.h>
typedef int Bool;
#define bool Bool
#define true 1
#define false 0
int main() {
bool f = false;
if (!f) {
printf("f is false");
}
return 0;
}
方法二:使用枚举
#include <stdio.h>
typedef enum {
true = 1,
false = 0
} bool;
int main() {
bool f = false;
if (!f) {
printf("f is false");
}
return 0;
}