← Back to C Course | Chapter 1: Introduction & Basics | Lesson 16 of 21

C Booleans

A boolean is like a light switch that can only be on or off, true or false. Older C had no special type for it, so C now offers a handy one in stdbool.h.
Syntax
c
#include <stdbool.h>

bool variable_name = true;  // or false

int flag = 1;  // classic style: 0 is false, non-zero is true

Booleans Before stdbool.h

Classic C has no dedicated boolean type -- conditions were always expressed with plain int, where the value 0 meant false and any non-zero value meant true, a convention that still underlies how every if and loop condition in C is evaluated today.

Example: Booleans Before stdbool.h

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare isValid and set it to 1
	int isValid = 1;
	// Check whether isValid
	if (isValid) {
		// Print formatted text to the screen
		printf("True (non-zero)");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

The bool Type from stdbool.h

Including <stdbool.h> brings in the bool type along with the true and false macros, which expand to 1 and 0 respectively -- it's a thin, readable layer over the same underlying integer representation, not a genuinely new machine type.

Example: The bool Type from stdbool.h

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include the bool type
#include <stdbool.h>
// Program execution starts in main()
int main() {
	// Declare isReady and set it to true
	bool isReady = true;
	// Print formatted text to the screen
	printf("%d", isReady);
	// Return 0 to signal that the program finished successfully
	return 0;
}

What _Bool Actually Is

bool is itself just a macro for _Bool, the real keyword the C99 standard introduced -- _Bool is guaranteed to store only 0 or 1, so assigning any non-zero value to it, like 10, is automatically normalized down to 1.

Example: What _Bool Actually Is

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	_Bool flag = 10;
	// Print formatted text to the screen
	printf("%d", flag);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Using Booleans in Conditions

A bool variable can be used directly in an if or while condition without comparing it to true explicitly -- writing if (isValid) is clearer and more idiomatic than if (isValid == true), which is redundant and easy to get wrong if reversed by mistake.

Example: Using Booleans in Conditions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include the bool type
#include <stdbool.h>
// Program execution starts in main()
int main() {
	// Declare isValid and set it to true
	bool isValid = true;
	// Check whether isValid
	if (isValid) {
		// Print formatted text to the screen
		printf("Valid");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Booleans as Function Return Values

Functions that answer a yes/no question read far more clearly when declared to return bool instead of int -- bool isPrime(int n) documents the function's intent at a glance, compared to an int-returning function where the meaning of 0 versus 1 has to be inferred.

Example: Booleans as Function Return Values

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include the bool type
#include <stdbool.h>
bool isPrime(int n) {
	return n == 2 || n == 3 || n == 5 || n == 7;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", isPrime(5));
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using bool without #include <stdbool.h> in older C standards, so it is undeclared.
  2. Assuming true and false are keywords in every C standard, when before C23 they are macros from stdbool.h.
  3. Assigning a non-zero value to a bool and expecting to get it back, when it becomes 1.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.