Truthiness: 0 is false, non-zero is true
What you'll learn: How C decides whether a value is "True" or "False" without using specific words.
In many modern languages, you have dedicated words like True and False. C is much more literal and grounded in how a computer's memory works. In C, "truth" isn't a special category; it is simply a number.
The Golden Rule of C Logic
When you write a conditional statement like an if block, C evaluates the expression inside the parentheses and looks at the resulting number. The rule is simple:
- Zero (0) is considered False.
- Anything else (non-zero) is considered True.
Think of a light switch. A 0 means the circuit is broken and the light is off (False). Any amount of voltage—whether it’s 1, 100, or -50—means the circuit is active and the light is on (True).
Seeing it in Action
Because C treats any non-zero number as true, you can pass raw integers directly into an if statement. You don't always need to compare them using symbols like == or >.
#include <stdio.h>
int main() {
int cookiesInJar = 5;
int billsToPay = 0;
// This evaluates to True because 5 is not 0
if (cookiesInJar) {
printf("You have cookies!\n");
}
// This evaluates to False because the value is 0
if (billsToPay) {
printf("You have bills to pay.\n");
}
return 0;
}
In the example above, the second printf never runs. C looks at billsToPay, sees a 0, and immediately skips the block.
The Negative Surprise
One common trap for beginners is assuming only positive numbers are "True." In C, even negative numbers are true. If you have -10 items in an inventory (perhaps a bug in your code), an if statement will still treat that as a "True" condition because it isn't zero.
This behavior is why C is so fast; it doesn't have to check if a value matches a specific "True" object. It just checks if the memory bit is empty (0) or has literally anything else in it.
Key takeaway
In C, 0 is the only way to represent False; every other number, positive or negative, is True.