Relational Operators
💻 Relational Operators in C (Easy + Clear Explanation)
In C (developed by Dennis Ritchie), relational operators are used to compare two values 🔍
👉 The result is always:
-
1→ True ✅ -
0→ False ❌
🔹 What are Relational Operators? 🤔
They are used to check conditions like:
- Equal or not
- Greater or smaller
👉 Mostly used in if conditions and loops
🔹 Types of Relational Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | a == b | True if equal |
!= | Not equal | a != b | True if not equal |
> | Greater than | a > b | True if a is bigger |
< | Less than | a < b | True if smaller |
>= | Greater or equal | a >= b | True if ≥ |
<= | Less or equal | a <= b | True if ≤ |
🔹 Examples of Each Operator
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("%d\n", a == b); // 0
printf("%d\n", a != b); // 1
printf("%d\n", a > b); // 1
printf("%d\n", a < b); // 0
printf("%d\n", a >= b); // 1
printf("%d\n", a <= b); // 0
return 0;
}
🔹 Output 🎉
0
1
1
0
1
0
🔹 Using Relational Operators in if Statement 🧠
#include <stdio.h>
int main() {
int a = 10, b = 5;
if (a > b) {
printf("a is greater");
}
return 0;
}
👉 Output:
a is greater
🔹 Important Points ⚠️
✔ == is comparison, = is assignment ❗
✔ Result is always 1 (true) or 0 (false)
✔ Used in conditions (if, while, for)
❗ Common Mistake
if (a = b) // ❌ wrong (assignment)
👉 Correct:
if (a == b) // ✅ correct (comparison)
🔹 Real-Life Example 🤔
👉 == → checking equality (marks same?)
👉 > → who scored more 📊
👉 < → who scored less
🔹 Final Summary 📝
- Used to compare values 🔍
- Return true (1) or false (0)
- Important for decision making
Comments
Post a Comment