Why are C booleans called _Bool?

后端 未结 3 1435
旧时难觅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: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.

提交回复
热议问题