#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("%d\n", a == b);
// 0 — false: 10 isn't 20
printf("%d\n", a != b);
// 1 — true: they differ
printf("%d\n", a < b);
// 1 — true
printf("%d\n", a > b);
// 0 — false
printf("%d\n", a <= 10);
// 1 — true: equal counts
printf("%d\n", b >= 25);
// 0 — false
return 0;
}Relational Operators in C: Comparing Values and Results
What is Relational Operators in C? Core Concept & Direct Summary
How do relational operators (`==`, `!=`, `<`, `>`, `<=`, `>=`) evaluate comparisons and yield integer boolean results?
Every decision a program makes starts with a question:Is the user 18 or older? Is the password correct? Is the score higher than the record?Before anyifcan decide, something has to compare.
That something is therelational operators— six symbols that compare two values and deliver a verdict. They're the quiet engines inside every condition, every loop, and every decision your programs will ever make.
They look trivially simple. And yet they hide two of C's most notorious traps — the=vs==disaster and the chained-comparison illusion (a < b < cdoesn't mean what you think). In this guide, you'll master all six operators, understand what they really return, and sidestep both traps permanently.
Let's compare.
How Does Relational Operators in C Work? In-Depth Mechanics & Architecture
The Six Relational Operators
What Comparisons Actually Return: The 1 and 0 System
Here's the detail that separates people whousethese operators from people whounderstandthem:
A relational expression doesn't produce "true" or "false" text. It produces the integer 1 (true) or 0 (false).
That output above — those bare1s and0s —isthe comparison result, printed raw. A comparison is just an expression with a numeric value, which means you can store it, return it, and calculate with it:
That second line looks like a party trick, but it's real C: each comparison yields 1 or 0, and adding them counts how many conditions passed. (You'll write clearer code with&&, but understandingwhythis works means you truly get the 1/0 system.)
This numeric nature is exactly what makesifstatements work —ifdoesn't need "true", it needsnon-zero:
Trap #1: The = vs == Disaster
The single most infamous typo in C:
Why does this compileandrun? Because assignment is an expression that evaluates to the assigned value.marks = 40sets marks to 40 and evaluates to40— non-zero — soifseestrue. The bug compiles silently, runs happily, and lies to you logically.
The defensive habit:some programmers writeif (40 == marks)— constant on the left. Thenif (40 = marks)becomes a compileerrorinstead of a silent bug. It's called a Yoda condition, and while modern compilers warn about assignment-in-if, the habit has saved countless programmers.
Trap #2: Chained Comparisons Don't Work
Coming from math class, thislooksperfectly reasonable:
Why does it misbehave? C reads left to right:
18 <= 15→ false →00 <= 60→ true →1✅ (0 is definitely ≤ 60!)
The comparison collapses into a 1-or-0 before the next comparison happens. The math-class chain simply doesn't exist in C. The correct version joins two separate comparisons with&&:
Every "between" check in C is two comparisons glued with&&.
Comparing Characters: It Works (Here's Why)
Because everycharis secretly an ASCII number, characters compare directly:
'a' > 'A'is true because lowercase letters have higher ASCII codes (97 > 65). This numeric nature is what makes alphabet loops, case conversion, and character classification possible in C.
The Float Warning: Never Compare Decimals With ==
One final trap — this one bites even experienced programmers:
Floating-point numbers are approximations.0.1 + 0.2doesn't equal0.3exactly — it equals something microscopically off, and==demandsexactness. The professional fix compares within a small tolerance:
Rule:compare floats for equality using a tolerance, never==.
A Complete Working Example: Grade Evaluator
Output:
Every branch in that ladder is powered by a relational operator doing its quiet 1-or-0 job.
Runnable C Code Example & Practical Implementation
#include <stdio.h>
int main() {
int score = 85;
int passingGrade = 60;
printf("Is passing: %d\
", score >= passingGrade);
printf("Is perfect: %d\
", score == 100);
printf("Needs improvement: %d\
", score < passingGrade);
return 0;
}Key Takeaways, Best Practices & Memory Safety Rules
-Wparentheses in compiler options to catch accidental assignments inside if statements.Frequently Asked Questions & Technical Interview Insights
Technical Interview & Viva Questions
Frequently asked C programming interview questionsQ: Why can comparing floating-point numbers with `==` produce unexpected bugs?
Q: What is a 'Yoda condition'?
Related Tools, Tutorials & Authoritative References
Recommended Interactive Tools
- Online C Compiler & Runner — Interactive online GCC sandbox to execute C code in real time.
- Online C Code Editor & IDE — Multi-file C programming environment with code formatting.
- C++ Programming Tutorial — Transition from procedural C to Object-Oriented C++ with STL.
- JavaScript Programming Tutorial — Master modern JavaScript fundamentals, DOM APIs, and async patterns.
Authoritative Standards & Citations
- ISO/IEC 9899 (C Language Standard) — Official international C specification by ISO/IEC JTC 1/SC 22/WG 14.
- GNU GCC Documentation — Official reference manual for the GNU Compiler Collection and C libraries.
- The C Programming Language (K&R) — Definitive classic work by Brian Kernighan & Dennis Ritchie at Bell Labs.
- C Reference Documentation (cppreference.com) — Verified reference covering C89 through C23 language features.