Using boolean values in C

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


Option 1

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

Option 2

typedef int bool;
enum { false, true };

Option 3

typedef enum { false, true } bool;

Option 4 (C99)

#include <stdbool.h>

Explanation

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


    A few thoughts on booleans in C:

    I'm old enough that I just use plain int s 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 in 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 mean 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 casee, 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 .


    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.

    链接地址: http://www.djcxy.com/p/25140.html

    上一篇: 我如何添加一个布尔字段到MySQL?

    下一篇: 在C中使用布尔值