“Relational operators” evaluate the relationship between two expressions, giving a true result (the value 1) or a false result (the value 0). C has six relational operators, which are listed in Table 6.2.
Table 6.2 Relational Operators
Operator | Description |
< | Less than |
<= | Less than or equal |
> | Greater than |
>= | Greater than or equal |
== | Equal |
!= | Not equal |
The “equality operator” (==), shown above, tests whether two expressions
are equal.
Don't confuse the equality operator with the assignment operator (=) discussed in the next section. The assignment operator sets one value equal to another, as we'll see shortly. For more information, see “Confusing Assignment and Equality Operators”.)
The C language gives the value 1 for true and 0 for false but recognizes any nonzero value as true. The following code fragment demonstrates this difference:
printf( "C generates %d for true\n", 2 == 2 );
printf( "C generates %d for false\n", 2 == 4 );
if( -33 )
printf( "C recognizes any nonzero value as true\n" );
The output from this code,
C generates 1 for true
C generates 0 for false
C recognizes any nonzero value as true
shows that the true expression (2 == 2) gives the value 1 and the false expression (2 == 4) gives the value 0. The last output line shows that C recognizes the nonzero value –33 as a true value.