Relational operators in C are used to compare values and determine the relationship between them. These operators return a result of either true (1) or false (0). Here are the commonly used relational operators along with their definitions, examples, and expected outputs:
1. Equal to (==):
Definition:
Checks if the values on both sides of the operator are equal.
Example:
#include <stdio.h>
int main() {
int a = 5, b = 5;
int result = (a == b);
printf(“%dn”, result);
return 0;
}
Output:
1 (True) because a is equal to b.
2. Not equal to (!=):
Definition:
Checks if the values on both sides of the operator are not equal.
Example:
#include <stdio.h>
int main() {
int a = 5, b = 3;
int result = (a != b);
printf(“%dn”, result);
return 0;
}
Output:
1 (True) because a is not equal to b.
3. Less than (<):
Definition:
Checks if the value on the left is less than the value on the right.
Example:
#include <stdio.h>
int main() {
int a = 3, b = 5;
int result = (a < b);
printf(“%dn”, result);
return 0;
}
Output:
1 (True) because a is less than b.
4. Greater than (>):
Definition:
Checks if the value on the left is greater than the value on the right.
Example:
#include <stdio.h>
int main() {
int a = 5, b = 3;
int result = (a > b);
printf(“%dn”, result);
return 0;
}
Output:
1 (True) because a is greater than b.
5. Less than or equal to (<=):
Definition:
Checks if the value on the left is less than or equal to the value on the right.
Example:
#include <stdio.h>
int main() {
int a = 3, b = 3;
int result = (a <= b);
printf(“%dn”, result);
return 0;
}
Output:
1 (True) because a is less than or equal to b.
6. Greater than or equal to (>=):
Definition:
Checks if the value on the left is greater than or equal to the value on the right.
Example:
#include <stdio.h>
int main() {
int a = 5, b = 5;
int result = (a >= b);
printf(“%dn”, result);
return 0;
}
Output:
(True) because a is greater than or equal to b.
These examples illustrate the use of relational operators in C, showing how they compare values and produce a boolean result (true or false). The output reflects the evaluation of the respective conditions.