Is C/C++ bool type always guaranteed to be 0 or 1 when typecast'ed to int?
Yes:
In C++ (§4.5/4):
An rvalue of type bool can be
converted to an rvalue of type int,
with false becoming zero and true
becoming one.
In C, when a value is converted to _Bool
, it becomes 0 or 1 (§6.3.1.2/1):
When any scalar value is converted to
_Bool, the result is 0 if the value compares equal to 0; otherwise, the
result is 1.
When converting to int
, it's pretty straight-forward. int
can hold 0 and 1, so there's no change in value (§6.3.1.3).
Does the C standard explicitly indicate truth value as 0 or 1?
Does the C standard explicitly indicate the truth values of
true
andfalse
as0
and1
respectively?
The C standard defines true
and false
as macros in stdbool.h
which expand to 1
and 0
respectively.
C11-§7.18:
The remaining three macros are suitable for use in
#if
preprocessing directives. They aretrue
which expands to the integer constant
1
,false
which expands to the integer constant
0
[...]
For the operators ==
and !=
, standard says
C11-§6.5.9/3:
The
==
(equal to) and!=
(not equal to) operators are analogous to the relational operators except for their lower precedence.108) Each of the operators yields1
if the specified relation is true and0
if it is false. The result has typeint
. For any pair of operands, exactly one of the relations is true.
Can I assume (bool)true == (int)1 for any C++ compiler?
According to the standard, you should be safe with that assumption. The C++ bool
type has two values - true
and false
with corresponding values 1 and 0.
The thing to watch about for is mixing bool
expressions and variables with BOOL
expression and variables. The latter is defined as FALSE = 0
and TRUE != FALSE
, which quite often in practice means that any value different from 0 is considered TRUE
.
A lot of modern compilers will actually issue a warning for any code that implicitly tries to cast from BOOL
to bool
if the BOOL
value is different than 0 or 1.
Can a C++ compiler represent bool internally with something other than 0 or 1?
The compiler can represent true however it likes as long as 1==(int)true
and 0==(int)false
The simplest way for the compiler to meet those requirements is to represent false
as all zero bits and true
as the least significant bit set, because then the "conversion" to int
doesn't involve changing anything.
But the compiler is free to represent true
as all bits set, and issue instructions to convert that to 1
when converted to int
, or even to represent true
as all zero bits and false
as all bits set or any other odd representation, as long it meets the requirements.
Is bool a native C type?
bool
exists in the current C - C99, but not in C89/90.
In C99 the native type is actually called _Bool
, while bool
is a standard library macro defined in stdbool.h
(which expectedly resolves to _Bool
). Objects of type _Bool
hold either 0 or 1, while true
and false
are also macros from stdbool.h
.
Note, BTW, that this implies that C preprocessor will interpret #if true
as #if 0
unless stdbool.h
is included. Meanwhile, C++ preprocessor is required to natively recognize true
as a language literal.
Using == outside of an if statement?
If b
is a bool
, you can assign the result of an expression to it. In this case, if the condition a == 18 % 13
holds, b
will become true
, otherwise false
.
Basically,
a == 18 % 13 - would yield b = true or b = 1
and
a != 18 % 13 - would yield b = false or b = 0
depending on the type of b
.
Related Topics
How to Compile SQLite with Icu
How to Compile Curlpp on Ubuntu
How to Write Log Base(2) in C/C++
How Does the Omp Ordered Clause Work
C++ Program Converts Fahrenheit to Celsius
How to Implement Multithread Safe Singleton in C++11 Without Using <Mutex>
Is There Any "Standard" Htonl-Like Function for 64 Bits Integers in C++
How to Convert Char* to Wchar_T*
Why Are My Struct's Members Not Properly Initialised Using '{}'
Linux C++ Error: Undefined Reference to 'Dlopen'
How to Write on a Virtual Webcam in Linux
Cannot Get Makefile to Build Each Object from Its Corresponding Source
What Is the Easiest Way to Parse an Ini File in C++