Topic 23 of 100 Operators in C Interactive Tutorial

Relational Operators in C: Comparing Values and Results

11 mins read Last updated: 22 Aug 2026 20,100 views
C Compiler 0.00%

What is Relational Operators in C? Core Concept & Direct Summary

Core Objective & Problem Statement

How do relational operators (`==`, `!=`, `<`, `>`, `<=`, `>=`) evaluate comparisons and yield integer boolean results?

Direct Answer Summary

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

OperatorNameAsks...ExampleResult
==Equal toare they the same?5 == 51(true)
!=Not equal toare they different?5 != 31(true)
>Greater thanis left bigger?5 > 31(true)
<Less thanis left smaller?5 < 30(false)
>=Greater or equalis left bigger or same?5 >= 51(true)
<=Less or equalis left smaller or same?4 <= 51(true)
main.c
Run Online
#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;
}

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:

main.c
Run Online
int isAdult =(age >= 18);
// stores 1 or 0
int bothPassed =(s1 >= 40) +(s2 >= 40);
// adds the results: 0, 1, or 2!

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:

main.c
Run Online
if (marks >= 40) {
    // marks >= 40 becomes 1 (or 0)...
    printf("Pass\n");
    // ...and if () runs on anything non-zero
}

Trap #1: The = vs == Disaster

The single most infamous typo in C:

main.c
Run Online
int marks = 30;
if (marks = 40) {
    // 😱 = ASSIGNS 40 — not a comparison!
    printf("Passed!\n");
    // always runs — and marks is now 40!
}

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.

main.c
Run Online
if (marks == 40) // ✅ == COMPARES — this is what you meant

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:

main.c
Run Online
int age = 15;
// "check if age is between 18 and 60" — the WRONG way
if (18 <= age <= 60) {
    // 😱 compiles fine, works WRONG
    printf("In range!\n");
    // runs?! even though 15 is out of range!
}

Why does it misbehave? C reads left to right:

  1. 18 <= 15→ false →0
  2. 0 <= 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&&:

main.c
Run Online
if (age >= 18 && age <= 60) {
    // ✅ both sides checked independently
    printf("In range!\n");
}

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:

main.c
Run Online
char grade = 'B';
if (grade == 'A') {
    /* not this time */
}
if (grade >= 'A' && grade <= 'Z') {
    printf("Uppercase letter\n");
    // runs — range check on characters!
}
printf("%d\n", 'a' > 'A');
// 1 — 'a' (97) beats 'A' (65)

'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:

main.c
Run Online
double x = 0.1 + 0.2;
if (x == 0.3) {
    printf("Equal\n");
} else {
    printf("NOT equal — x is actually %.20f\n", x);
    // x is 0.30000000000000004441 — NOT equal prints!
}

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:

main.c
Run Online
#include <math.h> // for fabs()
if (fabs(x - 0.3) < 0.000001) {
    // "close enough"
    printf("Equal (within tolerance)\n");
}

Rule:compare floats for equality using a tolerance, never==.

A Complete Working Example: Grade Evaluator

main.c
Run Online
#include <stdio.h>
int main() {
    int marks = 85;
    printf("Marks: %d\n", marks);
    if (marks >= 90) {
        printf("Grade: A+ (Outstanding)\n");
    } else if (marks >= 75) {
        printf("Grade: A (Excellent)\n");
        // this one runs
    } else if (marks >= 60) {
        printf("Grade: B (Good)\n");
    } else if (marks >= 40) {
        printf("Grade: C (Pass)\n");
    } else {
        printf("Grade: F (Fail)\n");
    }
    // relational results as numbers
    printf("Passed (1=yes, 0=no): %d\n", marks >= 40);
    printf("Distinction? %d\n", marks >= 90);
    return 0;
}

Output:

main.c
Run Online
Marks: 85
Grade: A(Excellent)
Passed(1=yes, 0=no): 1
Distinction? 0

Every branch in that ladder is powered by a relational operator doing its quiet 1-or-0 job.

Runnable C Code Example & Practical Implementation

main.c
Run Online
#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

Best Practice & Key Takeaways
Enable -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 questions
Q

Q: Why can comparing floating-point numbers with `==` produce unexpected bugs?

A: Due to binary floating-point rounding errors (e.g. `0.1 + 0.2 != 0.3` in binary precision). Floating-point comparisons should check if `fabs(a - b) < EPSILON`.
Q

Q: What is a 'Yoda condition'?

A: Placing the constant on the left (`if (5 == x)`) so that accidental assignment (`if (5 = x)`) triggers a compiler error.

Related Tools, Tutorials & Authoritative References

Recommended Interactive Tools

Authoritative Standards & Citations