Boolean-Integer Conversion in C
In the C programming language, it is often necessary to convert between different types of data. One common conversion is between boolean values and integers. The question arises: can we assume that the boolean value true is equivalent to the integer 1 for any C compiler?
The Answer
Yes, it is safe to assume that (bool)true == (int)1 for any C compiler. This is because of the following reasons:
Therefore, the expression true == 1 will evaluate to true, regardless of the C compiler being used. This behavior is specified in the C standard (4.7 [conv.integral]) which states that "If the source type is bool... true is converted to one."
The above is the detailed content of Is `true` Always Equivalent to `1` in C Boolean-Integer Conversions?. For more information, please follow other related articles on the PHP Chinese website!