bool類型


bool類型

//與C語言相比,C++添加了一種基本類型 —— bool類型,用來表示true和false
//true和false是字面值,可以通過轉變為int類型,true是1,false是0
#include <iostream>

using std::cout;
using std::endl;

int main()
{
  int x = true;
  int y = false;
  
  cout << "true = " << true << endl;
  cout << "x = " << x << endl;
  cout << "false = " << false << endl;
  cout << "y = " << y << endl;
  return 0;
}
//任何數字或指針值都可以隱式轉換為bool值
//任何非零值都將轉換為true,而零值轉換為false
#include <iostream>

using std::cout;
using std::endl;

int main()
{
  bool x = -100;
  bool y = 0;
  bool z = 100;
  bool yes = true;
  bool no = false;

  cout << "x = " << x << endl;
  cout << "y = " << y << endl;
  cout << "z = " << z << endl;
  cout << "yes = " << yes << endl;
  cout << "no = " << no << endl;

  return 0;
}
//一個bool類型的數據占據的內存空間大小為1
#include <iostream>

using std::cout;
using std::endl;

int main()
{
  cout << "sizeof(bool) = " << sizeof(bool) << endl;
  cout << "sizeof(true) = " << sizeof(true) << endl;
  cout << "sizeof(false) = " << sizeof(false) << endl;

  return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM