Truthiness: 0 vs non-zero
What you'll learn: How C determines whether a value represents "true" or "false" in logic checks.
In many modern programming languages, there is a dedicated bool type that can only be true or false. In C, however, logic is much more grounded in raw numbers. To master C, you need to understand that logic is simply a game of "zero vs. everything else."
The Gold Standard: Zero is False
In the eyes of a C compiler, the number 0 is the absolute definition of false.
Think of it like a light switch. If there is zero voltage flowing, the light is off. If you ask a C program, "Is this variable true?" and the variable contains 0, the program sees a dead circuit and concludes, "No, that is false." This applies to integers (0), characters ('\0'), and even floating-point numbers (0.0).
The Surprise: Anything Else is True
This is where beginners often get tripped up: C does not have a single value for true. Instead, any non-zero value is considered true.
If 0 is an empty glass, then a glass with one drop of water is "full enough" to be true. A glass with a gallon of water is also "true." Even a glass with a negative amount of water (if such a thing existed) would be "true."
In C, 1 is true. 100 is true. -5 is true. If the computer checks a value and finds even a tiny bit of numerical "signal" that isn't exactly zero, it treats that value as a logical "Yes."
Seeing it in Action
Even though we haven't covered if statements in depth yet, look at how the computer evaluates these numbers behind the scenes:
#include <stdio.h>
int main() {
int cookiesInJar = 5;
int billsToPay = 0;
// C sees 5 and thinks: "That's not zero, so it's TRUE!"
if (cookiesInJar) {
printf("The jar is not empty.\n");
}
// C sees 0 and thinks: "That's zero, so it's FALSE!"
if (billsToPay) {
printf("This will not print because 0 is false.\n");
}
return 0;
}
When you use relational operators (like 5 > 2), the computer does the math and generates a 1 for true. If you check 5 < 2, it generates a 0 for false. But remember: while the computer usually outputs 1 for true, it will accept any non-zero number you give it as a true statement.
Key takeaway
In C logic, 0 is False, and any non-zero value (positive or negative) is True.