CPP Boolean Data Type

C++ Tutorial

💻 CPP Boolean Data Type (bool) – Complete Beginner to Interview Guide

In CPP Boolean Data Type is used to store logical values.
It plays a very important role in conditions, loops, and decision-making.


1️⃣ What is Boolean Data Type? ⭐

In C++, the bool data type stores only two values:

  • true → logical 1

  • false → logical 0

Example

bool isPassed = true;

2️⃣ Declaring a Boolean Variable ⭐

Syntax

bool variable_name;

Example

bool status = false;

3️⃣ Boolean Example Program ⭐


 

Output

1

📌 By default:

  • true prints as 1

  • false prints as 0


4️⃣ Boolean with Conditions (if) ⭐⭐


 

Output

Adult

5️⃣ Boolean in Loops ⭐⭐


 

Output

Loop Running

6️⃣ Boolean with Relational Operators ⭐⭐

Relational operators return boolean values.


 

Output

1
0

7️⃣ Boolean with Logical Operators ⭐⭐⭐


 

Output

0
1
0

8️⃣ Display true / false Instead of 1 / 0 ⭐⭐

Use boolalpha:


 

Output

true
false

9️⃣ Boolean Size in Memory ⭐

cout << sizeof(bool);

📌 Size: 1 byte


🔟 Common Boolean Mistakes ❌

❌ Using True / False instead of true / false
❌ Confusing = with ==
❌ Assuming bool stores numbers other than 0 or 1
❌ Forgetting that conditions return boolean values


📌 Interview Questions (C++ Boolean)

Q1. What values can a bool store?
👉 true or false

Q2. What is the size of bool in C++?
👉 1 byte

Q3. What does boolalpha do?
👉 Prints true / false instead of 1 / 0

Q4. Can integers be used as boolean values?
👉 Yes (0 = false, non-zero = true)


✅ Summary

bool stores logical values
true → 1, false → 0
✔ Used in conditions and loops
✔ Result of comparisons & logical operators
✔ Very important for control flow

You may also like...