You will see these operators used frequently in decision-making statements like if, while, and loops.
Table of Contents
What Are Relational Operators?
Relational operators compare two operands and return a result based on their relationship.Types of Operators:
- Equal check: The == operator checks whether two values are equal and returns 1 if they are the same.
- Not equal: The != operator checks whether two values are different and returns 1 if they are not equal.
- Greater than: The operator checks if the left value is greater than the right value.
- Less than: The operator checks if the left value is smaller than the right value.
- Greater equal: The = operator checks if the left value is greater than or equal to the right value.
- Less equal: The = operator checks if the left value is less than or equal to the right value.
Basic Example of Relational Operators
Let’s look at a simple program to understand how these operators behave:
// C program to show relational operators
#include stdio.h
// Main function
int main()
{
int a = 10, b = 20;
printf("a == b: %d\n", a == b);
printf("a != b: %d\n", a != b);
printf("a b: %d\n", a b);
printf("a b: %d\n", a b);
printf("a = b: %d\n", a = b);
printf("a = b: %d\n", a = b);
return 0;
}
Output:
a == b: 0
a != b: 1
a b: 0
a b: 1
a = b: 0
a = b: 1
This shows that each comparison produces either 0 or 1 depending on the condition.
How Relational Operators Work?
Relational operators evaluate conditions between two expressions and return a result.
- Operand comparison: Relational operators compare two operands, which can be variables, constants, or expressions.
- Numeric result: The result of a relational operation is always an integer value, where 1 represents true and 0 represents false.
- Decision usage: These operators are mainly used in decision-making statements such as if, while, and loops.
Using Relational Operators in Decision Making
Relational operators are most commonly used inside conditional statements.
Example: Using if Statement
// C program to show relational operators
// in if-statement
#include stdio.h
// Main function
int main()
{
int num = 15;
if (num 10)
{
printf("Number is greater than 10\n");
}
return 0;
}
Output:
Number is greater than 10
Here, the if statement evaluates the relational expression and executes the block only if it is true.
Using Relational Operators with Expressions
Relational operators can also be used with arithmetic expressions.
// C program to show relational operators
// in expressions
#include stdio.h
// Main function
int main()
{
int x = 5, y = 3;
if ((x + y) (x - y))
{
printf("Sum is greater than difference\n");
}
return 0;
}
Output:
Sum is greater than difference
Explanation:
- Expression evaluation: The expressions on both sides are evaluated first before comparison.
- Final comparison: The relational operator compares the resulting values of the expressions.
Common Mistakes to Avoid
Even simple operators can lead to errors if not used carefully.
1. Assignment confusion: Many beginners mistakenly use = instead of ==, which assigns a value instead of comparing it.2. Boolean misunderstanding: Some users expect true or false as words, but C returns numeric values 1 and 0.
3. Chained conditions: Writing conditions like 10 x 1 does not work correctly in C and should be replaced with logical operators.
Correct version:
if (x 1 && x 10)
Conclusion
Relational operators are essential for making decisions in C programs. They allow you to compare values and control the flow of execution based on conditions.
Once you understand how each operator works and how they return results, writing logic becomes much easier. The key is to practice using them in real programs and avoid common mistakes like confusing assignment with comparison.
With consistent practice, relational operators will quickly become a natural part of your programming toolkit.
Frequently Asked Questions
1. What do relational operators return in C?
Relational operators return an integer value, where 1 indicates true and 0 indicates false.
2. Can relational operators be used with floating values?
Yes, relational operators can compare values of type int, float, and double.
3. What is the difference between == and =?
The == operator is used for comparison, while = is used for assigning values to variables.
4. Can multiple relational conditions be combined?
Yes, multiple conditions can be combined using logical operators such as && and ||.
5. Where are relational operators commonly used?
Relational operators are commonly used in conditional statements and loops to control program execution.
0 Comments