Relational operators in C are used to compare two values or variables. These operators return a boolean value (1 for true and 0 for false) based on the comparison.
List of Relational Operators
Operator | Description | Example | Result |
== | Equal to | a == b | True if a is equal to b |
!= | Not equal to | a != b | True if a is not equal to b |
> | Greater than | a > b | True if a is greater than b |
< | Less than | a < b | True if a is less than b |
>= | Greater than or equal to | a >= b | True if a is greater than or equal to b |
<= | Less than or equal to | a <= b | True if a is less than or equal to b |
Example Usage:
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
printf(“a == b: %d\n”, a == b); // False, outputs 0
printf(“a != b: %d\n”, a != b); // True, outputs 1
printf(“a > b: %d\n”, a > b); // False, outputs 0
printf(“a < b: %d\n”, a < b); // True, outputs 1
printf(“a >= b: %d\n”, a >= b); // False, outputs 0
printf(“a <= b: %d\n”, a <= b); // True, outputs 1
return 0;
}
Output:
a == b: 0
a != b: 1
a > b: 0
a < b: 1
a >= b: 0
a <= b: 1
Relational operators are critical for decision-making in conditional statements such as if, while, and for loops in C programming