Home >Backend Development >C++ >Does C Guarantee that `(bool)true` Equals `(int)1`?
Can C Compilers Guaranty (bool)true Equals (int)1?
The question arises: can it be assumed that (bool)true equates to (int)1 regardless of the C compiler being used?
Answer:
Yes, this assumption holds true for all C compilers. The casts in the expression are superfluous. The bool value directly converts to an int, as mandated by integral promotion, resulting in an int with a value of 1.
Reference:
According to 4.7 [conv.integral] / 4, "If the source type is bool... true is converted to one."
The above is the detailed content of Does C Guarantee that `(bool)true` Equals `(int)1`?. For more information, please follow other related articles on the PHP Chinese website!