
Booleans in C
In C, bool
(Boolean type) wasn't originally built into the language like in some modern languages, but starting from C99, you can use Booleans with the help of a standard header file.
? Using Booleans in C (C99 and later)
1. Include <stdbool.h>
Take an umbrella!
? Behind the Scenes
When you use bool
, it’s just a typedef for _Bool
, which is a built-in type in C99.
typedef _Bool bool;#define true 1#define bool int#define true 1#define false 0
But it’s better to just use <stdbool.h>
if your compiler supports it.
? Bonus Tip: Boolean Logic in C
bool a = true;bool b = false;bool result = a && !b; // AND + NOT
Logical operators:
&&
= AND||
= OR!
= NOT