Relational Operators and Logical Operators
Every comparison is an expression of type
int
that yields the value 1
or
0
. The value 1
means
“true” and 0
means “false.” Comparisons use the
relational operators listed in Table 1-11.
Operator |
Meaning |
Example |
Result: 1 (true) or 0 (false) |
< |
less than |
x < y |
|
<= |
less than or equal to |
x <= y |
|
> |
greater than |
x > y |
|
>= |
greater than or equal to |
x >= y |
|
== |
equal to |
x == y |
|
!= |
not equal to |
x != y |
|
The following operands are permissible for all relational operators:
Two operands with real arithmetic types. The usual arithmetic conversions may be performed on the operands.
Two pointers to objects of the same type.
The equality operators ==
and
!=
can also be used to compare complex numbers.
Furthermore, the operands may also be pointers to functions of the
same type. A pointer may also be compared with
NULL
or with a pointer to void
.
For example:
int cmp, *p1, *p2; . . . cmp = p1 < p2; // if p1 is less than p2, then cmp = 1; // otherwise cmp = 0.
Get C Pocket Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.