Boolean Variables

A Boolean variable can be used to hold the integer values 0 or 1, or the C++ literals true and false, which are implicitly promoted to the integers 0 and 1 whenever an arithmetic value is necessary. The type specifier to declare a Boolean variable is bool in C++. To declare a Boolean variable in C, use the bool macro, which is defined in the header file <stdbool.h>. A Boolean variable may not be further qualified by the specifiers signed, unsigned, short, or long.

C The Boolean type is unsigned and has the lowest ranking in its category of standard unsigned integer types. In simple assignments, if the left operand is a Boolean type, then the right operand must be either an arithmetic type or a pointer. An object declared as a Boolean type uses 1 byte of storage space, which is large enough to hold the values 0 or 1.

In C, a Boolean type can be used as a bit field type. If a nonzero-width bit field of Boolean type holds the value 0 or 1, then the value of the bit-field compares equal to 0 or 1, respectively.

Linux C The token bool is recognized as a keyword only when used in a vector declaration context and when the AltiVec language extensions have been enabled.

Linux _Bool uses 4 bytes of storage space, and is 4-byte aligned.

C++ Variables of type bool can hold either one of two values: true or false. An rvalue of type bool can be promoted to an integral type. A bool rvalue of false is promoted to the value 0, and a bool rvalue of true is promoted to the value 1.

The result of the equality, relational, and logical operators is of type bool: either of the Boolean constants true or false.

Use the type specifier bool and the literals true and false to make boolean logic tests. A boolean logic test is used to express the results of a logical operation. For example:

bool f(int a, int b)
{
  return a==b;
}
 

If a and b have the same value, f() returns true. If not, f() returns false.

Related References

IBM Copyright 2003