Boolean Conversions

C The conversion of any scalar value to type _Bool has a result of 0 if the value compares equal to 0; otherwise the result is 1.

C++You can convert integral, floating-point, arithmetic, enumeration, pointer, and pointer to member rvalue types to an rvalue of type bool. A zero, null pointer, or null member pointer value is converted to false. All other values are converted to true.

The following is an example of boolean conversions:

void f(int* a, int b)
{
  bool d = a;  // false if a == NULL
  bool e = b;  // false if b == 0
}

The variable d will be false if a is equal to a null pointer. Otherwise, d will be true. The variable e will be false if b is equal to zero. Otherwise, e will be true.

Related References

IBM Copyright 2003