HP C A.06.05 Reference Manual

Expressions and Operators
Logical Operators (&&, ||, !)
Chapter 5114
Truth Table for C's Logical Operators
In C, true is equivalent to any nonzero value, and false is equivalent to 0. The following table
shows the logical tables for each operator, along with the numerical equivalent. All of the
operators return 1 for true and 0 for false.
Examples of Expressions Using the Logical Operators
The following table shows a number of examples that use relational and logical operators. The
logical NOT operator has a higher precedence than the others. The AND operator has higher
precedence than the OR operator. Both the logical AND and OR operators have lower
precedence than the relational and arithmetic operators.
Table 5-10 Truth Table for C's Logical Operators
Operand Operator Operand Result
zero && zero 0
nonzero && zero 0
zero && nonzero 0
nonzero && nonzero 1
zero || zero 0
nonzero || zero 1
zero || nonzero 1
nonzero || nonzero 1
not applicable ! zero 1
! nonzero 0
Table 5-11 Examples of Expressions Using the Logical Operators
Given the following declarations:
int j = 0, m = 1, n = -1;
float x = 2.5, y = 0.0;
Expression Equivalent Expression Result
j && m (j) && (m) 0