Using boolean values in C

前端 未结 18 2178
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 12:51

C doesn\'t have any built-in boolean types. What\'s the best way to use them in C?

18条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 13:12

    If you are using C99 then you can use the _Bool type. No #includes are necessary. You do need to treat it like an integer, though, where 1 is true and 0 is false.

    You can then define TRUE and FALSE.

    _Bool this_is_a_Boolean_var = 1;
    
    
    //or using it with true and false
    #define TRUE 1
    #define FALSE 0
    _Bool var = TRUE;
    

提交回复
热议问题