Using boolean values in C

前端 未结 18 1810
被撕碎了的回忆
被撕碎了的回忆 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 12:59

    A boolean in C is an integer: zero for false and non-zero for true.

    See also Boolean data type, section C, C++, Objective-C, AWK.

    0 讨论(0)
  • 2020-11-22 12:59

    If you are using a C99 compiler it has built-in support for bool types:

    #include <stdbool.h>
    int main()
    {
      bool b = false;
      b = true;
    }
    

    http://en.wikipedia.org/wiki/Boolean_data_type

    0 讨论(0)
  • 2020-11-22 12:59

    It is this:

    #define TRUE 1
    #define FALSE 0
    
    0 讨论(0)
  • 2020-11-22 13:02

    Conditional expressions are considered to be true if they are non-zero, but the C standard requires that logical operators themselves return either 0 or 1.

    @Tom: #define TRUE !FALSE is bad and is completely pointless. If the header file makes its way into compiled C++ code, then it can lead to problems:

    void foo(bool flag);
    
    ...
    
    int flag = TRUE;
    foo(flag);
    

    Some compilers will generate a warning about the int => bool conversion. Sometimes people avoid this by doing:

    foo(flag == TRUE);
    

    to force the expression to be a C++ bool. But if you #define TRUE !FALSE, you end up with:

    foo(flag == !0);
    

    which ends up doing an int-to-bool comparison that can trigger the warning anyway.

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

    You can use a char, or another small number container for it.

    Pseudo-code

    #define TRUE  1
    #define FALSE 0
    
    char bValue = TRUE;
    
    0 讨论(0)
  • 2020-11-22 13:04

    A few thoughts on booleans in C:

    I'm old enough that I just use plain ints as my boolean type without any typedefs or special defines or enums for true/false values. If you follow my suggestion below on never comparing against boolean constants, then you only need to use 0/1 to initialize the flags anyway. However, such an approach may be deemed too reactionary in these modern times. In that case, one should definitely use <stdbool.h> since it at least has the benefit of being standardized.

    Whatever the boolean constants are called, use them only for initialization. Never ever write something like

    if (ready == TRUE) ...
    while (empty == FALSE) ...
    

    These can always be replaced by the clearer

    if (ready) ...
    while (!empty) ...
    

    Note that these can actually reasonably and understandably be read out loud.

    Give your boolean variables positive names, ie full instead of notfull. The latter leads to code that is difficult to read easily. Compare

    if (full) ...
    if (!full) ...
    

    with

    if (!notfull) ...
    if (notfull) ...
    

    Both of the former pair read naturally, while !notfull is awkward to read even as it is, and becomes much worse in more complex boolean expressions.

    Boolean arguments should generally be avoided. Consider a function defined like this

    void foo(bool option) { ... }
    

    Within the body of the function, it is very clear what the argument means since it has a convenient, and hopefully meaningful, name. But, the call sites look like

    foo(TRUE);
    foo(FALSE):
    

    Here, it's essentially impossible to tell what the parameter meant without always looking at the function definition or declaration, and it gets much worse as soon if you add even more boolean parameters. I suggest either

    typedef enum { OPT_ON, OPT_OFF } foo_option;
    void foo(foo_option option);
    

    or

    #define OPT_ON true
    #define OPT_OFF false
    void foo(bool option) { ... }
    

    In either case, the call site now looks like

    foo(OPT_ON);
    foo(OPT_OFF);
    

    which the reader has at least a chance of understanding without dredging up the definition of foo.

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