Relational & Logical Operators
True or false?Programs make decisions by asking questions: is the age at least 18? is the password correct? Relational operators compare values and give 1 (true) or 0 (false); logical operators combine those answers. In the lab below you watch conditions being decided step by step — including the parts C never even evaluates.
- falseis 0 · true is any other value
- ==compares · = assigns
- && ||stop as soon as the answer is known
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
The ideas behind the lab
Exactly what you watched happen, in plain steps.
True and false in C
C has no special true/false type in the classic language: it uses numbers. 0 means false and ANY other value means true. Comparisons give 1 or 0, and the logical operators &&, || and ! combine them — also giving 1 or 0. (stdbool.h adds bool, true and false, but they are still 1 and 0.)
int a = 7, b = 2; a > b 1 (true) a == b 0 (false) a != b 1 a > 5 && b > 5 0 both must be true a > 5 || b > 5 1 one is enough !(a > b) 0 NOT
| Operator | Meaning | Example | Result |
|---|---|---|---|
| < | less than | a < b | 0 |
| > | greater than | a > b | 1 |
| <= | less than or equal to | a <= 7 | 1 |
| >= | greater than or equal to | b >= 3 | 0 |
| == | equal to | a == 7 | 1 |
| != | not equal to | a != b | 1 |
| && | logical AND: both true | a > 5 && b > 5 | 0 |
| || | logical OR: at least one true | a > 5 || b > 5 | 1 |
| ! | logical NOT: reverses | !(a > b) | 0 |
Comparing values
- A comparison gives 1 if it is true and 0 if it is false.
- == compares; a single = ASSIGNS. if (a = 5) is always true and changes a.
- Characters are compared by their codes: 'a' < 'b' is 1, 'A' < 'a' is 1.
- Do not compare decimals with ==: 0.1 + 0.2 == 0.3 is 0. Check the difference instead.
- Comparisons cannot be chained: 1 < x < 5 means (1 < x) < 5, which is always 1.
Combining conditions
- p && q is true only when p AND q are both true.
- p || q is true when p OR q (or both) is true.
- !p is true when p is false.
- For a range write x > 1 && x < 5 — not 1 < x < 5.
- && is done before ||. Use parentheses to make your meaning clear.
Short-circuit evaluation
- && and || always evaluate the LEFT side first.
- If the left side of && is false, the result is 0 and the right side is NOT evaluated.
- If the left side of || is true, the result is 1 and the right side is NOT evaluated.
- Use it as a guard: b != 0 && a / b > 2 never divides by zero.
- Do not put actions you need (like i++) on the right side — they may never run.
Storing the answers to questions
Every line, explained.
#include <stdio.h>
int main(void)
{
int age = 20, marks = 72;
char grade = 'B';
int adult = age >= 18;
20 >= 18 is true, so adult is 1.
int passed = marks >= 40 && marks <= 100;
A range check: both comparisons are true, so passed is 1.
int top = grade == 'A' || marks > 90;
'B' == 'A' is 0 and 72 > 90 is 0, so top is 0.
int fail = !passed;
! reverses: passed is 1, so fail is 0.
int safe = marks != 0 && 100 / marks > 1;
The guard comes first. 100 / 72 is 1 (integer division), and 1 > 1 is false: safe is 0.
printf("%d %d %d %d %d\n", adult, passed, top, fail, safe);
Prints 1 1 0 0 0
return 0;
}
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
= instead of ==
if (marks = 100)
printf("Full marks!");
if (marks == 100)
printf("Full marks!");
marks = 100 stores 100 in marks; its value (100) is true, so the message ALWAYS prints. gcc -Wall warns: "suggest parentheses around assignment used as truth value".
A chained comparison
if (1 < x < 5)
if (x > 1 && x < 5)
(1 < x) gives 0 or 1, and both are < 5 — so the condition is always true.
& instead of &&
if (a > 0 & b > 0)
if (a > 0 && b > 0)
& is the bitwise AND (next module). It works here by luck but does not short-circuit, and with other values it gives wrong answers: 1 & 2 is 0.
Comparing decimals with ==
double total = 0.1 + 0.2; if (total == 0.3) // false
double total = 0.1 + 0.2; if (fabs(total - 0.3) < 1e-9)
0.1 + 0.2 is stored as 0.30000000000000004. Compare the difference with a small tolerance (fabs is in math.h).
Forgetting the parentheses with !
if (!age >= 18)
if (!(age >= 18)) // or: age < 18
! binds tighter than >=, so !age >= 18 means (!age) >= 18 — that is 0 or 1 compared with 18, always false.
Check your understanding
Answer each question — you will see the explanation straight away.
What is the value of 5 > 3 ?
A true comparison gives 1.
int a = 4; What is a == 4 && a != 4 ?
The second part is false, so && gives 0.
Which condition is true when x is between 10 and 20 (inclusive)?
Both comparisons must hold, joined with &&. Chained comparisons do not work in C.
In b != 0 && a / b > 1 with b = 0, what happens?
The left side is false, so && short-circuits: the division is skipped.
What is !0 ?
0 is false, so its logical NOT is true: 1.
Relational & Logical Operators quiz
Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.