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.

Table 1-11. The relational operators

Operator

Meaning

Example

Result: 1 (true) or 0 (false)

<

less than

x < y

1 if x is less than y

<=

less than or equal to

x <= y

1 if x is less than or equal to y

>

greater than

x > y

1 if x is greater than y

>=

greater than or equal to

x >= y

1 if x is greater than or equal to y

==

equal to

x == y

1 if x is equal to y

!=

not equal to

x != y

1 if x is not equal to y. In all other cases, the expression yields 0.

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.