读书人

C常见有关问题之用枚举类型模拟bool类

发布时间: 2013-10-18 20:53:13 作者: rapoo

C常见问题之用枚举类型模拟bool类型

ANSI C中是没有bool类型的,C99中引入了bool类型,具有true(也就是1)和false(也就是0)两种值,但是需要包含头文件stdbool.h之后才可以使用。

我们完全可以用枚举类型来模拟bool类型,如下所示:

typedef enum{    false, true}bool;bool test(int a){    if(a > 0){        return true;    }    else{        return false;    }}int main(void){    bool result;    result = test(1);    return 0;}

读书人网 >编程

热点推荐