C语言 使用bool

方法一:引入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;
}

 

posted on 2023-12-11 22:02  王景迁  阅读(41)  评论(0编辑  收藏  举报

导航