Sams Teach Yourself C in 21 Days

(singke) #1
Table 4.5 shows some examples of how relational operators might be used. These exam-
ples use literal constants, but the same principles hold with variables.

72 Day 4

“True” is considered the same as “yes,” which is also considered the same as
Note 1. “False” is considered the same as “no,” which is considered the same as 0.

TABLE4.4 C’s relational operators
Operator Symbol Question Asked Example
Equal == Is operand 1 equal to x == y
operand 2?
Greater than > Is operand 1 greater than x > y
operand 2?
Less than < Is operand 1 less than x < y
operand 2?
Greater than >= Is operand 1 greater than x >= y
or equal to or equal to operand 2?
Less than or <= Is operand 1 less than or x <= y
equal to operand 2?
Not equal != Is operand 1 not equal to x != y
operand 2?

TABLE4.5 Relational operators in use
What It
Expression How It Reads Evaluates To
5 == 1 Is 5 equal to 1? 0 (false)
5 > 1 Is 5 greater than 1? 1 (true)
5 != 1 Is 5 not equal to 1? 1 (true)
(5 + 10) == (3 * 5) Is (5 + 10) equal to (3 * 5)? 1 (true)

DOlearn how C interprets true and false.
When working with relational operators,
true is equal to 1 , and false is equal to 0.

DON’Tconfuse==, the relational opera-
tor, with =, the assignment operator. This
is one of the most common errors that C
programmers make.

DO DON’T


07 448201x-CH04 8/13/02 11:15 AM Page 72

Free download pdf