Relational operators: <, >, <=, >=, ==, !=
Relational operators in C programming are used to compare the relationship between two operands. They return a boolean value (`1` for true and `0` for false) based on the evaluation of the comparison. Here are the relational operators in C:
1. Less than (<):
Checks if the left operand is less than the right operand.
Example:
int result = 5 < 3; // result is 0 (false)
2.Greater than (>): Checks if the left operand is greater than the right operand.
Example:
int result = 5 > 3; // result is 1 (true)
3.Less than or equal to (<=):
Checks if the left operand is less than or equal to the right operand.
Example:
int result = 5 <= 3; // result is 0 (false)
4. Greater than or equal to (>=):
Checks if the left operand is greater than or equal to the right operand.
Example:
int result = 5 >= 3; // result is 1 (true)
5. Equal to (==):
Checks if the left operand is equal to the right operand.
Example:
int result = 5 == 3; // result is 0 (false)
6. Not equal to (!=):
Checks if the left operand is not equal to the right operand.
Example:
int result = 5 != 3; // result is 1 (true)
Relational operators are commonly used in conditional statements (e.g., `if`, `while`, `for`) to make decisions based on the comparison result. The result of a relational expression can be stored in a variable or used directly in conditional statements.
It's important to note that the operands of relational operators can be of any data type, including integers, floating-point numbers, characters, or pointers. However, comparing floating-point numbers for equality can sometimes lead to unexpected results due to precision limitations.
Example:
int num1 = 5;
int num2 = 3;
int result = (num1 > num2); // result is 1 (true)
if (num1 >= num2) {
// Execute this block if num1 is greater than or equal to num2
printf("num1 is greater than or equal to num2\n");
}
Relational operators allow you to compare values and make logical decisions based on those comparisons in your C programs.