Can anyone explain me what is the difference between the _Bool
and bool
data type in C?
For example:
_Bool x = 1;
bool y = true;
printf("%d", x);
printf("%d", y);
_bool is a keyword in C Programming language representing boolean data type. It is an alternative to bool in C. In fact, bool is an alias to _bool. This was done considering the historic usage of C as an attempt to maintain compatibility.
C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.
An introduction to how to use booleans in C C99, the version of C released in 1999/2000, introduced a boolean type. To use it, however, you need to import a header file, so I'm not sure we can technically call it “native”. Anyway, we do have a bool type.
These data types were added in C99. Since bool
wasn't reserved prior to C99, they use the _Bool
keyword (which was reserved).
bool
is an alias for _Bool
if you include stdbool.h
. Basically, including the stdbool.h
header is an indication that your code is OK with the identifier bool
being 'reserved', i.e. that your code won't use it for its own purposes (similarly for the identifiers true
and false
).
There is no difference.
bool
is a macro that expands to _Bool
in stdbool.h
.
And true
is a macro that expands to 1 in stdbool.h
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With