In C, relational operators are used to compare two values and return a Boolean value (either true or false). The following are the relational operators in C:
- == (equal to): The == operator is used to test if two values are equal. For example:
int a = 5;
int b = 2;
if (a == b) { // this condition is false // code goes here }
- != (not equal to): The != operator is used to test if two values are not equal. For example:
int a = 5;
int b = 2;
if (a != b) { // this condition is true // code goes here }
-
(greater than): The > operator is used to test if one value is greater than another. For example:
int a = 5;
int b = 2;
if (a > b) { // this condition is true // code goes here }
- < (less than): The < operator is used to test if one value is less than another. For example:
int a = 5;
int b = 2;
if (a < b) { // this condition is false // code goes here }
-
= (greater than or equal to): The >= operator is used to test if one value is greater than or equal to another. For example:
int a = 5;
int b = 2;
if (a >= b) { // this condition is true // code goes here }
- <= (less than or equal to): The <= operator is used to test if one value is less than or equal to another. For example:
int a = 5;
int b = 2;
if (a <= b) { // this condition is false // code goes here }
It is important to use the correct relational operator based on the type of comparison you want to perform. Using the wrong operator can result in errors or unexpected results in your code.