What does a comparison return in C? Not true. Not the compared value. A plain int — and this question from our C Programming Quiz App pins down exactly which one. It looks like the easiest question in the bank; the wrong options are where the learning is.
The Quiz Question
printf("%d", 3 == 3);
What is printed by this code?
- 3
- 0
- 1
- true
The Correct Answer: 1
The equality operator == — like every relational operator in C — yields an int: 1 when the comparison holds, 0 when it doesn’t. 3 equals 3, so the expression is 1. Verified on gcc 13.3 and Apple clang 21, warning-free:
$ gcc -Wall -Wextra eq.c && ./a.out 1
Because comparison results are ordinary integers, you can do arithmetic with them — a real (verified) curiosity that surprises people from stricter languages:
printf("%d\n", 3 == 4); /* 0 */
printf("%d\n", (3 == 3) + (2 == 2)); /* 2 — two 1s, added! */
Counting matches in a loop with count += (arr[i] == key); is this same property put to work.
Why Each Wrong Answer Is Wrong
Why not 3?
The comparison doesn’t hand back the value that matched — it hands back the verdict. Languages don’t generally return operands from == (that’s more of an ||/or behavior in Python/JavaScript, which C also doesn’t do).
Why not 0?
0 means “false” — the result of 3 == 4. If you picked 0, you may have confused ==‘s conventions with shell scripting, where exit code 0 means success. In C expressions, truth is 1 and 0 is false, full stop.
Why not “true”?
Two reasons. First, the format is %d — an integer is printed, so you’d never see letters. Second and deeper: for most of C’s life there was no true keyword at all. C99 added _Bool and a <stdbool.h> header that defines true as the integer 1; only C23 finally made true/false real keywords. Underneath, they’re still just 1 and 0 — and == yields int, not _Bool, even today.
The = vs == Landmine
One equals sign assigns, two compare — and because assignment is also an expression in C, typing = where you meant == often still compiles:
if (x = 0) /* assigns 0 to x, condition is always false! */
if (x == 0) /* compares — what you meant */
Both gcc and clang warn about the first form under -Wall (suggesting extra parentheses if you truly meant it). It’s such a classic bug that it has its own quiz question (#68) — that one’s coming in a future post. Until then, the habit that saves you: turn on -Wall and treat the “assignment as condition” warning as an error.
Frequently Asked Questions
What does a comparison like 3 == 3 return in C?
An int: 1 if the comparison is true, 0 if false. All six relational operators (==, !=, <, >, <=, >=) follow the same rule.
Does C have true and false?
Since C99, <stdbool.h> provides bool, true (1) and false (0); C23 made them keywords. But relational operators still yield plain int 1/0, and any nonzero value counts as true in conditions.
What happens if I write = instead of == in an if?
The assignment executes and its value becomes the condition — if (x = 0) is always false and silently zeroes x. It compiles; -Wall warns. Always double-check equals signs in conditions.
Related Reading
- Logical NOT in C – Why !0 Is 1
- Ternary Operator in C – How a < b ? a : b Picks the Minimum
- Modulo Operator in C – What 10 % 3 Returns
- C Aptitude Questions and Answers
Recommended Books
- The C Programming Language – Kernighan & Ritchie (India) | Amazon.com
- C Programming: A Modern Approach – K.N. King (India) | Amazon.com
This question is #128 in the C Programming Quiz App — 155 questions with explanations covering operators, pointers, memory, and more.
Download on Google Play →