Relational operators are symbols used to establish relationships between two values or expressions. These operators return a Boolean value, i.e., either true or false, depending on whether the relationship between the operands holds true or not. In most programming languages, including C, relational operators are primarily used in conditional statements and loops to control the flow of the program.
Operator | Description | Example | Result |
---|---|---|---|
Equal to (==) | Checks if two operands are equal. Returns true if they are equal, otherwise false. | 5 == 5 | true |
Not equal to (!=) | Checks if two operands are not equal. Returns true if they are not equal, otherwise false. | 7 != 3 | true |
Greater than (>) | Checks if the left operand is greater than the right operand. Returns true if it is, otherwise false. | 8 > 3 | true |
Less than (<) | Checks if the left operand is less than the right operand. Returns true if it is, otherwise false. | 4 < 2 | false |
Greater than or equal to (>=) | Checks if the left operand is greater than or equal to the right operand. Returns true if it is, otherwise false. | 6 >= 6 | true |
Less than or equal to (<=) | Checks if the left operand is less than or equal to the right operand. Returns true if it is, otherwise false. | 9 <= 3 | false |
#include <stdio.h> int main() { int a = 5, b = 5; if (a == b) { printf("%d is equal to %d\n", a, b); } else { printf("%d is not equal to %d\n", a, b); } return 0; }
5 is equal to 5
#include <stdio.h> int main() { int a = 7, b = 3; if (a != b) { printf("%d is not equal to %d\n", a, b); } else { printf("%d is equal to %d\n", a, b); } return 0; }
7 is not equal to 3
#include <stdio.h> int main() { int a = 8, b = 3; if (a > b) { printf("%d is greater than %d\n", a, b); } else { printf("%d is not greater than %d\n", a, b); } return 0; }
8 is greater than 3
#include <stdio.h> int main() { int a = 4, b = 2; if (a < b) { printf("%d is less than %d\n", a, b); } else { printf("%d is not less than %d\n", a, b); } return 0; }
4 is not less than 2
#include <stdio.h> int main() { int a = 6, b = 6; if (a >= b) { printf("%d is greater than or equal to %d\n", a, b); } else { printf("%d is neither greater than nor equal to %d\n", a, b); } return 0; }
6 is greater than or equal to 6
#include <stdio.h> int main() { int a = 9, b = 3; if (a <= b) { printf("%d is less than or equal to %d\n", a, b); } else { printf("%d is neither less than nor equal to %d\n", a, b); } return 0; }
9 is neither less than nor equal to 3