stdbool

Is (bool) cast reliably 0 or 1? [duplicate]

纵饮孤独 提交于 2020-02-03 14:34:52
问题 This question already has answers here : Casting int to bool in C/C++ (3 answers) Closed 3 months ago . From some reading on Stack Overflow, I gather that bool , as defined in stdbool.h , is a macro that expands to the built-in type _Bool , and that true is defined as 1 and false is defined as 0 . Is casting to bool guaranteed to return a value of 0 or 1 ? Might a _Bool variable, including cast rvalues, ever attain a value other than 0 or 1? The following code implies that _Bool variables,

Is (bool) cast reliably 0 or 1? [duplicate]

只愿长相守 提交于 2020-02-03 14:33:48
问题 This question already has answers here : Casting int to bool in C/C++ (3 answers) Closed 3 months ago . From some reading on Stack Overflow, I gather that bool , as defined in stdbool.h , is a macro that expands to the built-in type _Bool , and that true is defined as 1 and false is defined as 0 . Is casting to bool guaranteed to return a value of 0 or 1 ? Might a _Bool variable, including cast rvalues, ever attain a value other than 0 or 1? The following code implies that _Bool variables,

Why is C99's bool a macro rather than a typedef?

痞子三分冷 提交于 2019-12-24 08:59:26
问题 Why does the boolean type support introduced in C99 use the preprocessor rather than the language's own facilities? Specifically, why do we have: #define bool _Bool #define true 1 #define false 0 in <stdbool.h> rather than: typedef _Bool bool; enum { false = 0, true = 1 }; I guess the enum can be seen as a matter of taste. But - why not have a typedef? 回答1: From section 7.18/3 of the C11 specification: The remaining three macros are suitable for use in #if preprocessing directives. The

Boolean in C Programming

柔情痞子 提交于 2019-12-14 02:45:56
问题 So , unfortunately I encountered another problem with a program I'm trying to create. First of all I'm totally new to C Programming and I'm trying to create a Word Search . I have this piece of code which is in C++ and I'm trying to turn it into C : #include <iostream> using namespace std; int main() { char puzzle[5][5] = { 'A', 'J', 'I', 'P', 'N', 'Z', 'F', 'Q', 'S', 'N', 'O', 'W', 'N', 'C', 'E', 'G', 'L', 'S', 'X', 'W', 'N', 'G', 'F', 'H', 'V', }; char word[5] = "SNOW"; // The word we're

Why use <stdbool.h> instead of _Bool?

寵の児 提交于 2019-12-08 17:40:08
问题 Any time I had the need of a Boolean type I was told to either create one, or better yet, use stdbool.h . Since stdbool.h uses typedef bool _Bool , is there a reason to use the header instead just using type _Bool ? Is it just for the additional macros ( /* #define true 1 #define false 0 */ )? 回答1: The obvious type to add into the language was bool . But unfortunately, plenty of code was written that included bool in other shapes and forms. Recall that support for a boolean type was added