Truthy and Falsey Values

A Boolean data type is a data type that can represent two values: true or false.

In the C programming language, there is no built-in (a.k.a. primitive) Boolean data type prior to the C99 version of the language. Further, even programmers who use the C99 (or a later) version of the C programming language may choose not to use the built-in Boolean data type for compatibility with pre-C99 versions or out of habit. Therefore, C programmers who do not use a Boolean data type for a Boolean value often use an integer data type instead.

Because an integer data type can represent more than two values, the C programmer who uses an integer data type to behave as a Boolean data type must account for the possibility that a variable defined as an integer data type might contain values other than the integer values the programmer chooses to represent “true” and “false”, respectively.

The majority of the C programs I have analyzed which use an integer to represent a Boolean data type use the following method. This method takes advantage of the following convention in the C programming language:

When the C programming language tests an integer value in a context which requires the test to return “true” or “false” (e.g., in the expressions tested by “if” and “while” statements), the integer 0 is interpreted to be “false” and all other integers are interpreted to be “true”. In such a scenario, the integer 0 is called “falsey” and all other integers are called “truthy”. This convention of the C programming language applies regardless of the size of the integer and regardless of whether the integer is defined to be signed or unsigned. For example, signed integers less than 0 and signed and unsigned integers greater than 0 are interpreted to be “true” because they are not the integer 0.

Therefore, taking advantage of this convention, a C programmer will represent “false” as 0, and (often) a C programmer will represent “true” as 1, but they are not limited to 1 as the only “truthy” value. 

Note that C programmers often abstract the integers representing “true” and “false” by using C pre-processor macros, so that the actual values they choose to represent “true” and “false” are not exposed in the body of the source code, like the following:

#define TRUE 1
#define FALSE 0

int x = FALSE;
int y = TRUE;

Here are some examples of using integers in the Boolean context of an “if” statement:

int x = 0;
if (x) {
	/* will not be executed because x is not “truthy” */
} else {
	/* will be executed because x is “falsey” */
}

int x = 1;
if (x) {
	/* will be executed because x is “truthy” */
} else {
	/* will not be executed because x is not “falsey” */
}

int x = 72;
if (x) {
	/* will be executed because x is “truthy” */
} else {
	/* will not be executed because x is not “falsey” */
}

int x = -1;
if (x) {
	/* will be executed because x is “truthy” */
} else {
	/* will not be executed because x is not “falsey” */
}

int x = -29;
if (x) {
	/* will be executed because x is “truthy” */
} else {
	/* will not be executed because x is not “falsey” */
}

If the C programmer does not want to rely on this convention of “truthy” and “falsey” interpretation of integers in a Boolean context, the C programmer can explicitly test the value of the integer with a comparison operator. For example, each of the above examples can be re-written using the “x != 0” comparison (read as, x does not equal 0), as in the following:

int x = 0;
if (x != 0) {
	/* will not be executed because x != 0 is false */
} else {
	/* will be executed because x != 0 is false */
}

int x = 1;
if (x != 0) {
	/* will be executed because x != 0 is true */
} else {
	/* will not be executed because x != 0 is false */
}

int x = 72;
if (x != 0) {
	/* will be executed because x != 0 is true */
} else {
	/* will not be executed because x != 0 is false */
}

int x = -1;
if (x != 0) {
	/* will be executed because x != 0 is true */
} else {
	/* will not be executed because x != 0 is false */
}

int x = -29;
if (x != 0) {
	/* will be executed because x != 0 is true */
} else {
	/* will not be executed because x != 0 is false */
}