C++ में bool डेटा टाइप को int में बदलना बहुत सरल है। बूलियन मान true का इंटीजर समकक्ष 1 होता है, और false का 0। इसे टाइपकास्टिंग द्वारा किया जा सकता है।
उदाहरण कोड
#include <iostream>
using namespace std;
int main() {
bool myBool;
myBool = true;
cout << "The int equivalent of myBool (true) is: "
<< int(myBool) << endl;
myBool = false;
cout << "The int equivalent of myBool (false) is: "
<< int(myBool) << endl;
// अन्य उदाहरण
cout << "\nMore examples:\n";
cout << "true + true = " << (true + true) << endl; // 2
cout << "true - false = " << (true - false) << endl; // 1
cout << "sizeof(bool) = " << sizeof(bool) << " byte" << endl;
// इम्प्लिसिट कन्वर्जन
int x = true;
int y = false;
cout << "Implicit: int x = true; x = " << x << endl;
cout << "Implicit: int y = false; y = " << y << endl;
return 0;
}
आउटपुट
The int equivalent of myBool (true) is: 1 The int equivalent of myBool (false) is: 0 More examples: true + true = 2 true - false = 1 sizeof(bool) = 1 byte Implicit: int x = true; x = 1 Implicit: int y = false; y = 0
मुख्य बिंदु:
true→1(कोई भी नॉन-जीरो मान true माना जाता है)false→0- इम्प्लिसिट कन्वर्जन ऑटोमैटिकली होता है
- एक्सप्लिसिट कन्वर्जन के लिए
static_cast<int>(boolVal)याint(boolVal)उपयोग करें - C++ में
boolका साइज 1 बाइट होता है