int x = 4<5;
Completely portable. Standard conformant. bool
to int
conversion is implicit!
§4.7/4 from the C++ 11 or 14 Standard, §7.8/4 from the C++ 17 Standard, §7.3.9/2 from the 20 Standard says (Integral Conversion)
If the source type is bool, the value false
is converted to zero and
the value true
is converted to one.
As for C, as far as I know there is no bool
in C. (before 1999) So bool
to int
conversion is relevant in C++ only. In C, 4<5
evaluates to int
value, in this case the value is 1
, 4>5
would evaluate to 0
.
EDIT: Jens in the comment said, C99 has _Bool
type. bool
is a macro defined in stdbool.h
header file. true
and false
are also macro defined in stdbool.h
.
§7.16 from C99 says,
The macro bool
expands to _Bool.
[..] true
which expands to the integer constant 1
, false
which expands to the integer constant 0
,[..]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…