Comparison operators are used to compare two values.
Note: The return value of a comparison is either true (1) or false (0).
In the following example, we use the greater than operator (>) to find out if 5 is greater than 3:
#include<stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d", x > y); // returns 1 (true) because 5 is greater than 3
return 0;
}
Operator | Name | Example |
---|---|---|
== | Equal to | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
#include<stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d", x == y); // returns 0 (false) because 5 is not equal to 3
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d", x != y); // returns 1 (true) because 5 is not equal to 3
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d", x > y); // returns 1 (true) because 5 is greater than 3
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
int y = 3;
printf("%d", x < y); // returns 0 (false) because 5 is not less than 3
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
int y = 3;
// Returns 1 (true) because five is greater than, or equal, to 3
printf("%d", x >= y);
return 0;
}
#include<stdio.h>
int main() {
int x = 5;
int y = 3;
// Returns 0 (false) because 5 is neither less than or equal to 3
printf("%d", x <= y);
return 0;
}