In C, there are operators for comparing two entities:
Is a smaller than b?
These operators are mostly used in the conditional structures
we will see later:
if (a>b)
...
else
...
There are 6 comparison operators in C:
| Operator | name | Example | Type |
|---|---|---|---|
== |
is equal to | a == b |
binary |
!= |
is not equal to | a != b |
binary |
> |
greater than | a > b |
binary |
< |
less than | a < b |
binary |
>= |
greater than or equal to | a >= b |
binary |
<= |
less than or equal to | a <= b |
binary |
These operators can only return two values:
0 if the test is false ;1 if the test is true.Note: the result is always of type int, whatever the type of the operands.
= and the comparison operator ==.>= and != are operators,=> et =! are not operators.What is the value of the variable x after this instruction?
x = 12 > 8;
What should be the type of the variable x?
x = 12.5 <= 11.5;
int
Try again...
What is the greater than or equal to operator?
What is the operator to test if two variables are equal?
= with the comparison operator ==.
Try again...
What does the following program display?
a = 12;
printf ("%d", a == 16);
What does the following program display?
a = 12;
printf ("%d", a != 16);
1.
Try again...
What does the following program display?
printf ("%d", (5 > a) <= 3 );
(5 > a) can only return 0 or 1, which will always be less than 3.
Try again...