πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English
Advertisement

Comparison & Logical Operators

C comparison operators (==, !=, <, >) and logical operators (&&, ||, !).

πŸ“– What to learn on this page
βœ… Must-know essentials
  • ==, !=, <, >, <=, >= compare values
  • && (and), || (or), ! (not)
  • A comparison result is 0 (false) or 1 (true)
⭐ Read if you have time
  • Short-circuit: the right side is skipped when the result is known
  • & (bitwise AND) vs && (logical AND)
  • Avoid == on floats β€” use an epsilon

Comparison Operators — result is true (1) or false (0)

The result of a comparison is the integer 1 (true) or 0 (false).
OperatorMeaningResult for a=3, b=5
a == bEqual0 (false)
a != bNot equal1 (true)
a > bGreater0 (false)
a >= bGreater or equal0 (false)
a < bLess1 (true)
a <= bLess or equal1 (true)
Try it:
Result
β€”

Logical Operators — Combining Conditions

OperatorMeaningReads asExample
A && BA AND B (both true)ANDage >= 18 && age < 65
A || BA OR B (either true)ORday == 0 || day == 6
!ANOT ANOT!finished

Truth Tables

AND (&&)

ABA && B
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

OR (||)

ABA || B
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

== vs = — Critical!

Classic bug: writing if (a = 10) when you meant ==. This assigns 10 to a and then uses 10 as the condition, which is always true.
// BUG
if (a = 10) {  // = is assignment! a becomes 10 and condition is always true
  printf("Always runs\n");
}

// correct
if (a == 10) {  // == is comparison. True only when a equals 10
  printf("a is 10\n");
}
Memory hook: == is "check equal", = is "put into".

Ternary Operator (? :)

The ternary operator lets you write if-else in a single expression.
Syntax: condition ? value_if_true : value_if_false
// using if-else
int max;
if (a > b) {
    max = a;
} else {
    max = b;
}

// one line with ternary
int max = (a > b) ? a : b;
Common patterns:
UseExampleMeaning
Max of two values(a > b) ? a : ba if a is greater, otherwise b
Absolute value(x >= 0) ? x : -xx if non-negative, otherwise -x
Even / odd label(n%2==0) ? "even" : "odd"Can be passed directly to printf
Avoid divide-by-zero(b != 0) ? a/b : 0Divide if b isn't 0, otherwise 0
When to use: good for simple two-way assignments or printf arguments. For complex logic, if-else is more readable.
Avoid nesting: nested ternaries like (a>b) ? ((a>c)?a:c) : ((b>c)?b:c) are hard to read — use if-else instead.

Try It Yourself — Comparison & Logic

cmp.c
Output
Press "Run" to execute...
πŸ’‘ Try these ideas too
Advertisement

Related Lessons

Conditionals
if Statement
How to write if statements in C, with flowcharts for visual understanding.
Operators
Assignment & Increment
C assignment operators and increment (++) / decrement (--).
Operators
Arithmetic Operators
Visual guide to C arithmetic operators (+, -, *, /, %), including integer division pitfalls.
← Previous lesson
Lesson 9: Assignment & Increment
Next lesson →
Lesson 11: if Statement

Check Your Understanding

Check your understanding of this lesson!

Q1. Which C operator tests equality?

=
==
===

= is assignment; == is the equality (comparison) operator. === does not exist in C (that is a JavaScript operator).

Q2. What is !(1 && 0) ?

0 (false)
1 (true)
Error

1 && 0 = 0 (false), and negating it gives !0 = 1 (true).

Q3. What is 5 > 3 && 2 > 4 ?

1 (true)
0 (false)
Error

5 > 3 is true (1); 2 > 4 is false (0). && is true only if both are, so 1 && 0 = 0 (false).

Share this article
Share on X (Twitter) Share on Facebook Share on LINE Hatena Bookmark

Recommended Books for This Lesson

Learning the concepts here + doing exercises in a book is very effective

πŸ“˜
Suffer Through C
MMGames
A classic introductory book for beginners. Careful explanations cover the fundamentals.
View on Amazon
πŸ“—
New Clear C Programming: Introduction
Bohyoh Shibata
Plenty of diagrams and practice problems. Widely used as a university textbook.
View on Amazon
πŸ“™
The C Programming Language (2nd Edition)
B.W. Kernighan, D.M. Ritchie
Known as K&R. The original C text. Perfect for the next step after basics.
View on Amazon

* These are affiliate links. Your purchases help support this site.