Home >Backend Development >C++ >Does C Guarantee that `(bool)true` Equals `(int)1`?

Does C Guarantee that `(bool)true` Equals `(int)1`?

Susan Sarandon
Susan SarandonOriginal
2024-12-20 15:06:15333browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn