It is common for comparison functions to return 0 on "equals", so that they can also return a negative number for "less than" and a positive number for "greater than". strcmp() and memcmp() work like this.

It is, however, idiomatic for zero to be false and nonzero to be true, because this is how the C flow control and logical boolean operators work. So it might be that the return values chosen for this function are fine, but it is the function's name that is in error (it should really just be called compare() or similar).

Answer from caf on Stack Overflow
Top answer
1 of 8
57

It is common for comparison functions to return 0 on "equals", so that they can also return a negative number for "less than" and a positive number for "greater than". strcmp() and memcmp() work like this.

It is, however, idiomatic for zero to be false and nonzero to be true, because this is how the C flow control and logical boolean operators work. So it might be that the return values chosen for this function are fine, but it is the function's name that is in error (it should really just be called compare() or similar).

2 of 8
17

This upside-down-world is common with process error returns. The shell variable $? reports the return value of the previous program to execute from the shell, so it is easy to tell if a program succeeds or fails:

$ false ; echo  true ; echo $?
0

This was chosen because there is a single case where a program succeeds but there could be dozens of reasons why a program fails -- by allowing there to be many different failure error codes, a program can determine why another program failed without having to parse output.

A concrete example is the aa-status program supplied with the AppArmor mandatory access control tool:

   Upon exiting, aa-status will set its return value to the
   following values:

   0   if apparmor is enabled and policy is loaded.

   1   if apparmor is not enabled/loaded.

   2   if apparmor is enabled but no policy is loaded.

   3   if the apparmor control files aren't available under
       /sys/kernel/security/.

   4   if the user running the script doesn't have enough
       privileges to read the apparmor control files.

(I'm sure there are more widely-spread programs with this behavior, but I know this one well. :)

🌐
CS UIC
cs.uic.edu › ~jbell › CourseNotes › CPlus › Decisions.html
CPlus Course Notes - Decisions and Branching
C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true. C++ is backwards compatible, so the C-style ...
🌐
Reddit
reddit.com › r/cs50 › is 0 true or false to c?
r/cs50 on Reddit: Is 0 True or False to C?
March 26, 2012 -

I am getting confused. In the "week 2, continued" lecture, prof. Dave said that "returning 0 indicates all is well effectively leaving us with 4 billion things that coould go wrong". So I concluded from here that 0 is True to C. But in the book "Absolute beginner to C" by Greg Perry in chapter 19, it is written "False is 0 to C". If 0 is False, then why does the main function returns 0 by default? Please help me settle this once and for all.

🌐
Reddit
reddit.com › r/askmath › how did 1 and 0 come to be associated with true and false?
r/askmath on Reddit: how did 1 and 0 come to be associated with true and false?
July 14, 2025 -

How did 1 and 0 come to be associated with True and False?

Nowadays in mathematics and computer science it is common for the number 1 to be associated with truth, and 0 with falsehood. This strikes me as a non-obvious, and so I'm wondering how this came to be taken for granted? Is this actually universal or are there other standards I'm not aware of? Is there a good reason for this association or is it mere convention?

🌐
Reddit
reddit.com › r/learnpython › why do only >=0 numbers have a boolean equivalent?
r/learnpython on Reddit: Why do only >=0 numbers have a boolean equivalent?
October 30, 2024 -

I'm curious about the reasoning behind this logic:

>>> bool(1)
True
>>> bool(0)
False
>>> bool(-1)
True
>>> 1 == True
True
>>> 0 == True
False
>>> -1 == True
False
>>> -1 == False
False

Any number can be converted to a bool. Non-zero evaluates to True while zero evaluates to False. Similarly, positive numbers are equivalent to True and zero is equivalent to False. This all makes intuitive sense. What confuses is me is the last two lines. Apparently, negative numbers have the boolean conversion defined but not the equivalence. Why is this?

Top answer
1 of 4
11

I think your question is a false dichotomy, at least in a lot of cases; modern high-level languages1 tend not to allow conversion between booleans and integer types, so this should be considered an implementation detail and it doesn't matter how a specific implementation happens to do it. Taking this further, "register" is an implementation detail as well.

1. C and C++ are not "modern" in this context!

2 of 4
6

Because the actual register cannot store a bit, and the register has no type,

You are probably thinking about a general purpose register. However, CPUs typically also have flags to store certain results, and those flags are typically one bit containing a true/false value. Many CPU architectures have something like a CMP (compare) instruction, which can compare two values for equality, and which stores the result in one of those flags. Some CPUs allow those flags to be accessed directly, some only indirecly (like with JE/JNE (jump if equal/not equal) instructions), and some present a collection of flags as a single status register. Still, you can think of the flag as a single boolean register.

It's also not right to say that register don't have a type. You typically have different types of registers, some for integers, some for floating points. In hardware, there is typically a set of registers that are all 32 or 64 bits, but the instruction set might expose logical registers that are 8 or 16 bits wide. At the hardware level there are also no ones and zeroes, it's some amount of charge or voltage, or the absence of it, and it doesn't even have to be that a high voltage means a 1.

Mathematics usually uses 0 to represent false and 1 to represent true

I think it's computer science that does that, not mathematics.

Some mathematicians believe that 0 and -1 should be used from the perspective of truth tables

Some operations on booleans have an equivalent in operations on integers. For example, if you use 0 for false and 1 for true, then a logical AND is equivalent to multiplication of those integer values. You can then also see that this no longer works if you use 0 for true and 1 for false.

When working with registers of a fixed width, you are working in a ring with modular arithmetic, and usually signed numbers are stored in two's complement. That means -1 is equivalent to all bits in that register being 1. This can make some things easier, for example in the following piece of C code:

bool a = …;
int x = 123;
int y = a ? x : 0;

The compiler could then replace the conditional expression with a bitwise AND operation:

int y = x & (int)a;

Which could result in much faster assembly being generated. However, since using just 1 and 0 for true and false is so common, most CPUs provide instructions to handle that as fast as storing booleans as -1 and 0 would.

Also, note that in C and languages that use the same ABI, 0 is false but when converting an int or other similar type to bool, any non-zero value is considered true. And while the ABI says that a boolean is stored or passed as a parameter, the value should be exactly 1 or 0, the compiler is allowed to do anything it wants behind the scenes as long as the observable end result is the same as what the language specification says. So it might actually store true as a non-zero value other than 1 if that is more expedient and nothing would break.

The solution adopted by the shell is 0 to represent true and other values ​​to represent false

Not exactly; the result of a command is not a boolean, it is an error code, where 0 means no error. The same goes for errno.

Mainstream FFIs all default 0 to false

Because C defined false to be zero, and most FFIs use C's ABI.

CPU initialization is all 0 initialization,

Not necessarily, and especially not the memory. A BIOS or operating system's first task is usually to zero all memory. The CPU has defined reset values for some of the registers, and some of those reset values might not be zero. When a program is started by the operating system, it's also not the case that all registers are zero.

What is true is that often the CPU provides a fast way to set a register to zero, either by having specific instructions for that or by having a special register that is always zero, and that you can copy into other registers. And even if there is nothing explicit, you can almost always XOR a register with itself.

Find elsewhere
🌐
Quora
quora.com › Is-0-true-or-false-in-C
Is 0 true or false in C++? - Quora
Answer (1 of 4): 0 is neither true nor false in C++. C++ is of strong type system language. And 0 is a literal of integer type when true and false are values of boolean type. You probably tried to mean the case of int to bool conversion, are you?
🌐
Sololearn
sololearn.com › en › Discuss › 156484 › why-is-boolean-value-of-0-false
Why is boolean value of 0 false? | Sololearn: Learn to code for FREE!
To machines doing checks on power, 1 returns true 0 returns false. If you perform checks on all other numbers you will receive a true because the value is set, not necessarily because the value itself is true... unless it is.
🌐
Wyzant
wyzant.com › resources › ask an expert
1 = false and 0 = true? | Wyzant Ask An Expert
April 4, 2019 - There's no law saying you can't ... to mean "false" and 0 is defined to mean "true" except that expressions like if (is_equals(... )) { .... will surely not do what the API user expects, as to their reasoning only they could tell you. Since C has no native exception system/error handling it's ...
🌐
Polytechnique
lix.polytechnique.fr › ~liberti › public › computing › prog › c › C › CONCEPT › true_false.html
True or False
The concept of an expression evaluating to true or false is one of the corner stones of C. BUT the language derives true and false in an unusual way. Basicly there is no boolean value. The number 0 is considered to be false and all other numbers are considered to be true....
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › why-0-is-equal-to-false-in-javascript
Why “0” is equal to false in JavaScript ? - GeeksforGeeks
July 12, 2025 - In JavaScript "0" is equal to false because "0" is of type string but when it tested for equality the automatic type conversion of JavaScript comes into effect and converts the "0" to its numeric value which is 0 and as we know 0 represents ...
🌐
Wikipedia
en.wikipedia.org › wiki › Boolean_data_type
Boolean data type - Wikipedia
3 days ago - Instead, any value can behave as Boolean in Boolean context (condition of if or while statement, argument of && or ||, etc.). The number 0, the strings "0" and "", the empty list (), and the special value undef evaluate to false.
🌐
Jinja
jinja.palletsprojects.com › en › stable › templates
Template Designer Documentation — Jinja Documentation (3.1.x)
This can be disabled by passing case_sensitive=True. Changed in version 3.1: Added the case_sensitive parameter. Sorting and grouping is case-insensitive by default, matching other filters that do comparisons. ... Changed in version 3.0: Added the default parameter. Changed in version 2.6: The attribute supports dot notation for nested access. jinja-filters.indent(s: str, width: int | str = 4, first: bool = False...
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.3 documentation
1 month ago - By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object. [1] If one of the methods raises an exception when called, the exception is propagated and the object does not have a truth value (for example, NotImplemented). Here are most of the built-in objects considered false: ... Operations and built-in functions that have a Boolean result always return 0 or False for false ...
🌐
scikit-learn
scikit-learn.org › stable › modules › generated › sklearn.metrics.confusion_matrix.html
confusion_matrix — scikit-learn 1.8.0 documentation
For binary classification, compute ... false negative and true positive counts per threshold. ... Wikipedia entry for the Confusion matrix (Wikipedia and other references may use a different convention for axes). ... >>> from sklearn.metrics import confusion_matrix >>> y_true = [2, 0, 2, 2, 0, ...
🌐
W3Schools
w3schools.com › python › python_booleans.asp
Python Booleans
Any list, tuple, set, and dictionary are True, except empty ones. ... In fact, there are not many values that evaluate to False, except empty values, such as (), [], {}, "", the number 0, and the value None.
🌐
Microsoft Support
support.microsoft.com › en-us › office › vlookup-function-0bbc8083-26fe-4963-8ab8-93a18ad188a1
VLOOKUP function - Microsoft Support
=VLOOKUP(What you want to look up, where you want to look for it, the column number in the range containing the value to return, return an Approximate or Exact match – indicated as 1/TRUE, or 0/FALSE).
🌐
CS UIC
cs.uic.edu › ~jbell › CourseNotes › C_Programming › Decisions.html
C Programming Course Notes - Decisions and Branching
To make life easier, C Programmers typically define the terms "true" and "false" to have values 1 and 0 respectively.