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

การแปลง Bool เป็น Int ใน C++


ที่นี่เราจะดูวิธีการแปลง bool เป็นค่า int เทียบเท่าใน C++ Bool เป็นชนิดข้อมูล (Data Type) ใน C++ และเราสามารถใช้คีย์เวิร์ด true หรือ false สำหรับมัน ถ้าเราต้องการแปลง bool เป็น int เราสามารถใช้ Type Casting ได้ ค่า true จะเป็น 1 เสมอ และค่า false จะเป็น 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