Relational Operators in C
Programs make decisions all the time: Is the age 18 or more? Are the marks above 40? Is the password correct? To answer these, you compare values using relational operators.
In this lesson you will learn the six relational operators, what they return, and the most common bug in C: writing = when you mean ==.
What You Will Learn
- The 6 relational (comparison) operators.
- Why they return 1 or 0.
- The difference between
=and==. - Why comparing decimal numbers with
==is risky. - How to compare characters.
Relational Operators in C Explained
Exam definition: Relational operators compare two values. The result is 1 if the comparison is true and 0 if it is false. C has six relational operators: ==, !=, >, <, >=, <=.
| Operator | Meaning | Example (a = 10, b = 20) | Result |
|---|---|---|---|
== | Equal to | a == b | 0 |
!= | Not equal to | a != b | 1 |
> | Greater than | a > b | 0 |
< | Less than | a < b | 1 |
>= | Greater than or equal to | a >= 10 | 1 |
<= | Less than or equal to | b <= 15 | 0 |
Difference between = and ==
= (assignment) | == (equality) |
|---|---|
| Stores a value | Compares two values |
x = 5 puts 5 into x | x == 5 asks "is x equal to 5?" |
| Result is the stored value (5) | Result is 1 (true) or 0 (false) |
| Assignment operator | Relational operator |
Memory trick: One = gives (puts a value in). Two == ask (are they the same?). Read x == 5 aloud as "x is equal to 5".
Syntax of Relational Operators
value1 operator value2 /* result: 1 (true) or 0 (false) */
if (age >= 18) { ... } /* used mostly in conditions */
int is_pass = marks >= 40; /* or stored as 1 or 0 */
Example 1: All Relational Operators
#include <stdio.h>
int main(void)
{
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 >= 10: %d\n", a >= 10);
printf("b <= 15: %d\n", b <= 15);
printf("'A' < 'B': %d\n", 'A' < 'B');
return 0;
}
Output
a == b : 0 a != b : 1 a > b : 0 a < b : 1 a >= 10: 1 b <= 15: 0 'A' < 'B': 1
Code Explanation
- Each comparison gives 1 (true) or 0 (false), and printf prints that number.
a >= 10: 10 is equal to 10, so "greater than or equal" is true.'A' < 'B': characters are compared by their ASCII codes: 65 < 66, so true.
How Comparisons Work
- C has no separate true/false type in classic C. It uses numbers: 0 means false, and any non-zero value means true.
- Relational operators always produce exactly 1 or 0.
- In
if (x = 5), the assignment gives 5, which is non-zero, so the condition is always true. That is why the=vs==mistake is so dangerous: no error, just wrong behaviour. - Decimal numbers are stored approximately.
0.1 + 0.2 == 0.3is false in C. For decimals, check if the difference is very small instead.
Example 2: The = vs == Bug
#include <stdio.h>
int main(void)
{
int marks = 45;
if (marks = 100) /* BUG: assigns 100, always true */
printf("Full marks!\n");
printf("marks is now %d\n", marks);
if (marks == 45) /* correct comparison */
printf("marks is 45\n");
else
printf("marks is not 45\n");
return 0;
}
Output
Full marks! marks is now 100 marks is not 45
The student had 45, but the program printed "Full marks!" and changed the marks to 100. Compiling with -Wall warns you about this: suggest parentheses around assignment used as truth value.
Important Points to Remember
- 6 relational operators:
== != > < >= <=. - They return 1 (true) or 0 (false).
=assigns,==compares.- In C, 0 is false and any non-zero value is true.
- Don't compare floats with
==.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
if (x = 0) | Assigns 0; the condition is always false | if (x == 0) |
=> or =< | Syntax error | Write >= and <= (the = comes second) |
if (10 < x < 20) | Always true: (10 < x) is 0 or 1, and both are < 20 | if (x > 10 && x < 20) |
if (a == 0.3) with floats | May be false even when it looks equal | Check fabs(a - 0.3) < 0.0001 |
Comparing strings with == | Compares addresses, not text | Use strcmp() |
Relational Operators Practice Questions
- Name the six relational operators in C.
- What do relational operators return?
- Write two differences between
=and==. - What is the output of
printf("%d", 5 > 3 > 1);? - Why is
if (x = 5)always true?
Show answers
==,!=,>,<,>=,<=.- 1 if the comparison is true, 0 if it is false.
=stores a value and returns it;==compares two values and returns 1 or 0.- 0.
5 > 3is 1, then1 > 1is 0. - It assigns 5 to x, and the expression's value is 5, which is non-zero, so it is true.
Coding Exercise
Read a person's age and print 1 if they can vote (18 or more) and 0 if not, using a relational operator only (no if).
Show solution
#include <stdio.h>
int main(void)
{
int age;
printf("Enter age: ");
scanf("%d", &age);
printf("Can vote: %d\n", age >= 18);
return 0;
}
Enter age: 17 Can vote: 0
Quick Quiz: Relational Operators in C (MCQ)
-
What does a true comparison return in C?
A) true B) 1 C) −1 D) yes
-
Which checks if x is NOT equal to 5?
A)
x =! 5B)x <> 5C)x != 5D)x !== 5 -
Output of
printf("%d", 7 <= 7);A) 0 B) 1 C) 7 D) Error
-
=is a ______ operator.A) Relational B) Logical C) Assignment D) Bitwise
-
Output of
printf("%d", 'a' > 'A');A) 0 B) 1 C) 32 D) Error
Show answers
- B: 1.
- C:
x != 5 - B: 1.
- C: assignment.
- B: 1 ('a' is 97, 'A' is 65).
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Operators | == != > < >= <= |
|---|---|
| Result | 1 (true) or 0 (false) |
= | Assignment: stores |
== | Equality: compares |
| True in C | Any non-zero value |
5 > 3 > 1 | 0 |
| Compare strings | strcmp(), not == |
Frequently asked questions
- What is the difference between = and == in C?
=assigns a value;==compares two values and returns 1 or 0.- What do relational operators return?
- 1 for true, 0 for false (an int).
- Why is if (x = 5) a bug?
- It assigns 5 instead of comparing, and 5 is non-zero, so the condition is always true.
- Why does 10 < x < 20 not work in C?
- It becomes (10 < x) < 20, i.e. 0 or 1 < 20, which is always true. Use
x > 10 && x < 20.
Remember
- One = gives, two == ask.
- 0 = false, non-zero = true.
Next Topic
Next lesson: Logical Operators in C. You will learn to join conditions with AND, OR and NOT.