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

OperatorMeaningExampleResult
==Equal toa == bTrue if equal
!=Not equala != bTrue if not equal
>Greater thana > bTrue if a is bigger
<Less thana < bTrue if smaller
>=Greater or equala >= bTrue if ≥
<=Less or equala <= bTrue 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

Popular posts from this blog

Introduction to Computer

History of Computer

Computer Generation