Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting int to bool in C/C++

I know that in C and C++, when casting bools to ints, (int)true == 1 and (int)false == 0. I'm wondering about casting in the reverse direction...

In the code below, all of the following assertions held true for me in .c files compiled with Visual Studio 2013 and Keil µVision 5. Notice (bool)2 == true.

What do the C and C++ standards say about casting non-zero, non-one integers to bools? Is this behavior specified? Please include citations.

#include <stdbool.h> #include <assert.h>  void TestBoolCast(void) {     int i0 = 0, i1 = 1, i2 = 2;      assert((bool)i0 == false);     assert((bool)i1 == true);     assert((bool)i2 == true);      assert(!!i0 == false);     assert(!!i1 == true);     assert(!!i2 == true); } 

Not a duplicate of Can I assume (bool)true == (int)1 for any C++ compiler?:

  1. Casting in the reverse direction (int --> bool).
  2. No discussion there of non-zero, non-one values.
like image 232
cp.engr Avatar asked Jul 22 '15 00:07

cp.engr


People also ask

Can you convert an int to a bool?

Since both integer and boolean are base data types, we can convert an integer value to a boolean value using the Convert class. The Convert. ToBoolean() method converts an integer value to a boolean value in C#.

Is bool true 1 or 0?

C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.

Which is faster bool or int?

Surprisingly, g(bool) generates more asm instructions! Does it mean that if(bool) is little slower than if(int) ? I used to think bool is especially designed to be used in conditional statement such as if , so I was expecting g(bool) to generate less asm instructions, thereby making g(bool) more efficient and fast.

How do you use boolean in C++?

In C++, we use the keyword bool to declare this kind of variable. Let's take a look at an example: bool b1 = true; bool b2 = false; In C++, Boolean values declared true are assigned the value of 1, and false values are assigned 0.


2 Answers

0 values of basic types (1)(2)map to false.

Other values map to true.

This convention was established in original C, via its flow control statements; C didn't have a boolean type at the time.


It's a common error to assume that as function return values, false indicates failure. But in particular from main it's false that indicates success. I've seen this done wrong many times, including in the Windows starter code for the D language (when you have folks like Walter Bright and Andrei Alexandrescu getting it wrong, then it's just dang easy to get wrong), hence this heads-up beware beware.


There's no need to cast to bool for built-in types because that conversion is implicit. However, Visual C++ (Microsoft's C++ compiler) has a tendency to issue a performance warning (!) for this, a pure silly-warning. A cast doesn't suffice to shut it up, but a conversion via double negation, i.e. return !!x, works nicely. One can read !! as a “convert to bool” operator, much as --> can be read as “goes to”. For those who are deeply into readability of operator notation. ;-)


1) C++14 §4.12/1 “A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. For direct-initialization (8.5), a prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.”
2) C99 and C11 §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.”

like image 127
Cheers and hth. - Alf Avatar answered Sep 24 '22 15:09

Cheers and hth. - Alf


The following cites the C11 standard (final draft).

6.3.1.2: When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.

bool (mapped by stdbool.h to the internal name _Bool for C) itself is an unsigned integer type:

... The type _Bool and the unsigned integer types that correspond to the standard signed integer types are the standard unsigned integer types.

According to 6.2.5p2:

An object declared as type _Bool is large enough to store the values 0 and 1.

AFAIK these definitions are semantically identical to C++ - with the minor difference of the built-in(!) names. bool for C++ and _Bool for C.

Note that C does not use the term rvalues as C++ does. However, in C pointers are scalars, so assigning a pointer to a _Bool behaves as in C++.

like image 20
too honest for this site Avatar answered Sep 24 '22 15:09

too honest for this site