C Booleans

Boolean Basics

C doesn't have a native bool type in traditional C (pre-C99). Instead:

  • 0 means false
  • non-zero means true
Loading...
Output:

Using _Bool and <stdbool.h>

Starting in C99, you can use the _Bool type. If you include <stdbool.h>, you can write:

  • bool – same as _Bool
  • true – defined as 1
  • false – defined as 0
Loading...
Output:

Logical Conditions

You use boolean expressions in control structures like if, while, and logical operators:

  • && – AND
  • || – OR
  • ! – NOT
Loading...
Output:

Common Mistakes

  • Confusing = with ==: if (x = 1) is assignment, not comparison!
  • Using bool without including <stdbool.h>
  • Assuming true or false are built-in without headers
Loading...
Output:

Practice Question

What will this print?

A. 0
B. 1
C. 5
Loading...
Output:

Hint: || returns 1 (true) if either side is non-zero.

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic