Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
karthikeya Boyini

Here we will see how to convert bool to int equivalent in C++. Bool is a datatype in C++, and we can use true or false keyword for it. If we want to convert bool to int, we can use typecasting. Always true value will be 1, and false value will be 0.

Example

#include <iostream>
using namespace std;
main() {
   bool my_bool;
   my_bool = true;
   cout << "The int equivalent of my_bool is: " << int(my_bool) << endl;
   my_bool = false;
   cout << "The int equivalent of my_bool is: " << int(my_bool);
}

Output

The int equivalent of my_bool is: 1
The int equivalent of my_bool is: 0

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.