3

When you use the if statement in C/C++, or any other logical operator, is the operand you pass to the statement cast to an integer for evaluation?

My current understanding was that the operand passed to the statement is cast to an integer to test whether or not it is non-zero (true), and that if you pass a pointer, this can be cast to an integer to evaluate with a 0/null value being defined as false.

I was under the impression that C++'s standard Bool values were simply typedef of an unsigned char with a value of 0 and 1.

Can anyone explain what's actually happening behind the scenes with this behavior?

TryinHard
  • 4,078
  • 3
  • 28
  • 54
yfaxs
  • 73
  • 1
  • 6
  • 1
    Please read [this SO post](http://stackoverflow.com/questions/1479100/how-is-if-statement-evaluated-in-c). – Tim Biegeleisen Jul 31 '15 at 05:20
  • 1
    C or C++? *C++'s standard Bool values were simply typedefs of an unsigned char with a value of 0 and 1*, that's simply not true. – Yu Hao Jul 31 '15 at 05:21
  • As I said "I was under the impression that..." Thank you for clearing that up. – yfaxs Jul 31 '15 at 05:28

2 Answers2

10

In C++ bool is a standalone type that has nothing to do with unsigned char. And in C++ language the expression under if is indeed implicitly cast to bool type. Note: to bool specifically, not to int. For scalar types other than bool, the implicit conversion to bool is essentially defined through non-equality comparison to literal 0. I.e. values that compare non-equal are converted to true, while values that compare equal - to false.

In other words, in C++ statement

if (a)

is interpreted as

bool tmp = (bool) a;
// `tmp` is either `true` or `false`
if (tmp)

which for scalar types is interpreted as

bool tmp = a != 0;
if (tmp)

Note that this works "as expected" for pointer types as well. But instead of converting the pointer to int type, it actually works the other way around: it converts literal 0 to the proper pointer type.

For other types it could be something different, like a call to a user-defined conversion operator operator bool().


In C language the expression under if has to have scalar type. And it is implicitly compared to constant 0. Note that this does not involve converting the controlling expression it to int. Comparison to constant 0 is defined separately for all scalar types. E.g. for int it has its natural meaning, while for pointer types it is interpreted as comparison to null pointer. Now, the result of such comparison in C has type int and evaluates to either 0 or 1. That 0 or 1 is what controls what if branch is taken.

In other words, in C statement

if (a)

is immediately interpreted as

int tmp = a != 0;
// `tmp` is either `0` or `1`
if (tmp)

Additionally, your assumption that null pointer produces a zero int value when converted to int type is incorrect. Neither language makes such guarantee. Null pointer is not guaranteed to be represented by zero address value and is not guaranteed to produce zero value when converted to int.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Thank you for a great answer! This makes alot of sense. I also mistakenly assumed that C and C++ would handle this in the exact same way. I guess I made the assumption regarding 'bool' and 'int' from the fact that sizeof(bool) is '1' on most systems. Thanks once again. – yfaxs Jul 31 '15 at 05:32
  • To clarify the last 2 paras for OP: `ptr != 0` converts `0` to pointer type, which is guaranteed to generate a null pointer, so `if ( ptr )` does exactly test for non-null pointer. (As opposed to the idea of converting `ptr` to `int` and testing against `0`, which does not happen and would not be guaranteed to work even if it did, as you explain). – M.M Jul 31 '15 at 05:38
  • That's quite interesting! Thanks for pointing that out! Makes alot of sense too. It's interesting that I was looking at this whole issue from the opposite angle haha. Thanks for your help! – yfaxs Jul 31 '15 at 05:41
  • Don't you mean _explicitly_ cast to `bool` type? If you only provide `explicit operator bool` from `object` (like `std::unique_ptr` does for example), `if (object)` still works. – bobobobo Oct 22 '21 at 02:41
0

In C++, the condition in an if statement is converted to a bool.

From the C++11 Standard:

6.4 Selection statements

4 The value of a condition that is an initialized declaration in a statement other than a switch statement is the value of the declared variable contextually converted to bool (Clause 4). If that conversion is ill-formed, the program is ill-formed.

A different section, 4.12 Boolean conversion, of the standard talks about conversions to bool.

4.12/1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. A prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.

R Sahu
  • 204,454
  • 14
  • 159
  • 270