Can I Assume Equivalence Between (bool)true and (int)1 Across Compilers?
The question at hand revolves around whether the equality (bool)true == (int)1 holds true universally for C compilers. In other words, can one safely assume the interchangeability of these two expressions?
According to the provided response, the answer is a resounding "Yes". The casts themselves are superfluous because of integral promotion. Integral promotion dictates that bool values are automatically promoted to int, with true specifically being promoted to the integer value 1.
This behavior is detailed in the C standard's section 4.7 [conv.integral] / 4, which explicitly states that "If the source type is bool... true is converted to one."
Therefore, the expression true == 1 is essentially equivalent to 1 == 1, which evaluates to true regardless of the compiler being used.
The above is the detailed content of Can I Always Assume `(bool)true` is Equivalent to `(int)1` in C ?. For more information, please follow other related articles on the PHP Chinese website!