Why are C booleans called _Bool?

后端 未结 3 1436
旧时难觅i
旧时难觅i 2021-02-12 13:03

Why does C use the word _Bool to define boolean values? Whereas they use the word float for floats and not _Float?

Furthermore, wh

相关标签:
3条回答
  • 2021-02-12 13:07

    C did not originally have a Boolean type, it was added in the 1999 version of the language (C99). At that point, C++ was already standardized (in 1998) to use the type bool, with keywords false and true. To keep the C Boolean type separate from the one in C++, as well as preventing the new name from breaking old C code, it was named _Bool.

    The reason why it was named with an underscore followed by an upper-case letter, is because such an identifier was already guaranteed not to exist in compiler, library or user code, by 7.1.3:

    All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

    "Reserved for any use" meaning reserved for future versions of the C language.

    Therefore, all new language keywords that have been added to the language since C99 are named with underscore followed by first letter upper-case. Other examples from C99 are the types _Complex and _Imaginary.


    For the cases where code compatibility with C++ was desired, the header <stdbool.h> was created. It contains the macro bool, which expands to _Bool. And also the macros false and true that expand to 0 and 1.

    Though note that booleans are not fully integrated in the C language, as they are in C++. In C++, an expression such as a == b gives a result of type bool, with the value true or false. In C it gives a result of type int, with the value 1 or 0. This is for backwards-compatibility reasons with old C code.

    0 讨论(0)
  • _Bool was not originally in C, but was added in the 1999 C Standard. If it had been called bool then a large amount of existing code would break because many projects made their own type alias bool already.

    The C89 standard set aside identifiers starting with _ followed by upper-case character as reserved for implementation use. This is why new features added to C always start with such names. _Complex, _Alignof and _Static_assert are other examples.

    There is also a header <stdbool.h> which aliases bool to _Bool and defines true and false ; this header can be included by new projects or by projects that didn't already define bool.

    0 讨论(0)
  • 2021-02-12 13:33

    As for ...

    Furthermore, why does bool have to be included, why isn't part of the basic functionality, like float?

    ... I observe that although you need to include stdbool.h to get bool, that's a convenience and C++-compatibility feature, not an essential. bool is an alias for _Bool, and in C99 and later you have _Bool automatically, but even that is non-essential. In C, any integer or pointer value can be interpreted as a boolean, with 0 or NULL being interpreted as false and all other values being interpreted as true. This was how boolean values were handled in C from the beginning, and it still works in implementations conforming to the latest standard.

    In fact, type _Bool itself is just a special case of this very behavior: it is an integer type whose minimal requirements are that it be able to represent the values 0 and 1. In boolean context, it works just the same as any other integer type.

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