Nearby lessons

29 of 124

C - Relational Operators

Relational Operators is one of the foundational topics in C programming. This lesson explains Relational (Comparison) Operators (== != < > <= >=) and Program: all six relational operators with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Relational (Comparison) Operators (== != < > <= >=)

These compare two values and give either true (1) or false (0). C has no boolean word for beginners — it uses 1 for true and 0 for false.

OperatorMeaningExample
==Equal toa == b
!=Not equal toa != b
<Less thana < b
>Greater thana > b
<=Less than or equal toa <= b
>=Greater than or equal toa >= b
In simple words: a comparison always answers one question — true or false. In C, true is printed as 1 and false as 0. No other answer is possible.

Program: all six relational operators

Example02
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int a = 10, b = 20;
6 
7 printf("%d == %d : %d\n", a, b, a == b); // 0
8 printf("%d != %d : %d\n", a, b, a != b); // 1
9 printf("%d < %d : %d\n", a, b, a < b); // 1
10 printf("%d > %d : %d\n", a, b, a > b); // 0
11 printf("%d <= %d : %d\n", a, b, a <= b); // 1
12 printf("%d >= %d : %d\n", a, b, a >= b); // 0
13}
Output

10 == 20 : 0
10 != 20 : 1
10 <  20 : 1
10 >  20 : 0
10 <= 20 : 1
10 >= 20 : 0
      
📝 Key Takeaways
  • Operators: arithmetic (+ - * / %), relational (== != < > <= >=), logical (&& || !), assignment (= += -= *= /= %=), ++ --, ternary ?:.
  • Each operator family has its own complete program — copy and run one at a time.
  • Integer division drops decimals: 5/2 = 2; use 5.0/2 for 2.5.
  • % gives the remainder and works only on integers.
  • a++ uses then increases; ++a increases then uses.
  • Ternary: condition ? value1 : value2.
  • Use brackets to control the order of evaluation.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1