What does the C++ language definition promise about casting a char to bool then back to char again?
char original = 255;
bool next = original;
char final = next;
Also, what do most compilers do in this case beyond what the language guarantees?
string char and bool are 3 data type of variable. bool use less memory space than other and have 2 only state true and false, char use only 1 byte and the max lenght is 1 character string is an array of char so it takes as much space as the character contained.
To explicitly convert a value to bool, use the (bool) or (boolean) casts. However, in most cases the cast is unnecessary, since a value will be automatically converted if an operator, function or control structure requires a bool argument.
This will give a value of zero or one, depending on whether the original value was zero or non-zero.
Converting to bool
gives a value of true
or false
:
4.12 A zero value, null pointer value, or null member pointer value is converted to
false
; any other value is converted totrue
.
Converting back to char
converts false
to zero, and true
to one:
4.7/4 If the source type is
bool
, the valuefalse
is converted to zero and the valuetrue
is converted to one.
Integral values converted to bool
result in either true
or false
(4.12), and bool
converted to integral values results in either 1
or 0
(4.5(6)). See Chapter 4 (Standard Conversions).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With