Using boolean values in C

前端 未结 18 1811
被撕碎了的回忆
被撕碎了的回忆 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;
    
    0 讨论(0)
  • 2020-11-22 13:16

    From best to worse:

    Option 1 (C99)

    #include <stdbool.h>
    

    Option 2

    typedef enum { false, true } bool;
    

    Option 3

    typedef int bool;
    enum { false, true };
    

    Option 4

    typedef int bool;
    #define true 1
    #define false 0
    

    Explanation

    • Option 1 will work only if you use C99 and it's the "standard way" to do it. Choose this if possible.
    • Options 2, 3 and 4 will have in practice the same identical behavior. #2 and #3 don't use #defines though, which in my opinion is better.

    If you are undecided, go with #1!

    0 讨论(0)
  • 2020-11-22 13:16

    You could use _Bool, but the return value must be an integer (1 for true, 0 for false). However, It's recommended to include and use bool as in C++, as said in this reply from daniweb forum, as well as this answer, from this other stackoverflow question:

    _Bool: C99's boolean type. Using _Bool directly is only recommended if you're maintaining legacy code that already defines macros for bool, true, or false. Otherwise, those macros are standardized in the header. Include that header and you can use bool just like you would in C++.

    0 讨论(0)
  • 2020-11-22 13:17

    Just a complement to other answers and some clarification, if you are allowed to use C99.

    +-------+----------------+-------------------------+--------------------+
    |  Name | Characteristic | Dependence in stdbool.h |        Value       |
    +-------+----------------+-------------------------+--------------------+
    | _Bool |   Native type  |    Don't need header    |                    |
    +-------+----------------+-------------------------+--------------------+
    |  bool |      Macro     |           Yes           | Translate to _Bool |
    +-------+----------------+-------------------------+--------------------+
    |  true |      Macro     |           Yes           |   Translate to 1   |
    +-------+----------------+-------------------------+--------------------+
    | false |      Macro     |           Yes           |   Translate to 0   |
    +-------+----------------+-------------------------+--------------------+
    

    Some of my preferences:

    • _Bool or bool? Both are fine, but bool looks better than the keyword _Bool.
    • Accepted values for bool and _Bool are: false or true. Assigning 0 or 1 instead of false or true is valid, but is harder to read and understand the logic flow.

    Some info from the standard:

    • _Bool is NOT unsigned int, but is part of the group unsigned integer types. It is large enough to hold the values 0 or 1.
    • DO NOT, but yes, you are able to redefine bool true and false but sure is not a good idea. This ability is considered obsolescent and will be removed in future.
    • Assigning an scalar type (arithmetic types and pointer types) to _Bool or bool, if the scalar value is equal to 0 or compares to 0 it will be 0, otherwise the result is 1: _Bool x = 9; 9 is converted to 1 when assigned to x.
    • _Bool is 1 byte (8 bits), usually the programmer is tempted to try to use the other bits, but is not recommended, because the only guaranteed that is given is that only one bit is use to store data, not like type char that have 8 bits available.
    0 讨论(0)
  • 2020-11-22 13:19

    This is what I use:

    enum {false, true};
    typedef _Bool bool;
    

    _Bool is a built in type in C. It's intended for boolean values.

    0 讨论(0)
  • 2020-11-22 13:22

    Anything nonzero is evaluated to true in boolean operations, so you could just

    #define TRUE 1
    #define FALSE 0
    

    and use the constants.

    0 讨论(0)
提交回复
热议问题