Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

การแปลงบูลเป็น int ใน C ++


ที่นี่เราจะมาดูวิธีการแปลง bool เป็น int ที่เทียบเท่าใน C ++ Bool เป็นประเภทข้อมูลในภาษา C++ และเราสามารถใช้ จริง หรือ เท็จ คีย์เวิร์ดสำหรับมัน ถ้าเราต้องการแปลง bool เป็น int เราสามารถใช้ typecasting ค่าจริงจะเป็น 1 เสมอ และค่าเท็จจะเป็น 0

ตัวอย่าง

#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);
}

ผลลัพธ์

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