Bool to int conversion in C++

C++Server Side ProgrammingProgramming

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
raja
Published on 03-May-2019 07:14:50
Advertisements