CPP Boolean Data Type
💻 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
2️⃣ Declaring a Boolean Variable ⭐
Syntax
Example
3️⃣ Boolean Example Program ⭐
Output
📌 By default:
-
trueprints as1 -
falseprints as0
4️⃣ Boolean with Conditions (if) ⭐⭐
Output
5️⃣ Boolean in Loops ⭐⭐
Output
6️⃣ Boolean with Relational Operators ⭐⭐
Relational operators return boolean values.
Output
7️⃣ Boolean with Logical Operators ⭐⭐⭐
Output
8️⃣ Display true / false Instead of 1 / 0 ⭐⭐
Use boolalpha:
Output
9️⃣ Boolean Size in Memory ⭐
📌 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
