Representing Boolean true/false
A Boolean value is one that evaluates to true
or false
. On some systems, YES
and yes
are equivalent to true
, while NO
and no
are equivalent to false
. For instance, Is today Wednesday? evaluates to true
only 1 out of 7 days. On the other 6 days, it evaluates to false
.
Before C99, there was no explicit type for a Boolean. A value of any type that is 0 (exactly 0) is considered as also evaluating to a Boolean false
. Any value other than exactly 0
(a bit pattern of only 0s) will evaluate to a Boolean value of true
. Real numbers rarely, if ever, evaluate exactly to 0
, especially after any kind of operation on them. Therefore, these data types would almost always evaluate to true
and, hence, would be poor choices as a Boolean substitute.
Since C99, a _Bool
type was added, which, when evaluated, will always evaluate to only 0
or 1
. When we include the stdbool.h
file, we are able to use the bool
type as well; this is a bit cleaner than using the cumbersome...