BOOL 和 bool

來源:互聯網
上載者:User

一、BOOL和bool的區別

  1、類型不同

  BOOL為int型

  bool為布爾型

  2、長度不同

  bool只有一個位元組

  BOOL長度視實際環境來定,一般可認為是4個位元組

  3、取值不同

  bool取值false和true,是0和1的區別; false可以代表0,但true有很多種,並非只有1。

  如果數個bool對象列在一起,可能會各佔一個bit,這取決於編譯器。

  BOOL是微軟定義的typedef int BOOL(在windef.h中)。       與bool不同,它是一個三值邏輯,TRUE/FALSE/ERROR,傳回值為大於0的整數時為TRUE,傳回值為0時候,為FALSE,傳回值為-1時為ERROR。

  Win32 API中很多傳回值為BOOL的函數都是三值邏輯。比如GetMessage().

  BOOL GetMessage(

  LPMSG lpMsg, // message information

  HWND hWnd, // handle to window

  UINT wMsgFilterMin, // first message

  UINT wMsgFilterMax // last message);

  If the function retrieves a message other than WM_QUIT, the return value is nonzero.

  If the function retrieves the WM_QUIT message, the return value is zero.

  If there is an error, the return value is -1.

  *********************************************************************************************************************************************

編輯本段二、布爾型變數bool

  bool是布爾型變數,也就是邏輯型變數的定義符,類似於float,double等,只不過float定義浮點型,double定義雙精確度浮點型。
在objective-c中提供了相似的類型BOOL,它具有YES值和NO值。

  布爾型變數的值只有 真 (true) 和假 (false)。

  布爾型變數可用於邏輯運算式,也就是“或”“與”“非”之類的邏輯運算和大於小於之類的關係運算,邏輯運算式運算結果為真或為假。

  bool可用於定義函數類型為布爾型,函數裡可以有 return TRUE; return FALSE 之類的語句。

  布爾型運算結果常用於條件陳述式,

  if (邏輯運算式)

  {

  如果是 true 執行這裡;

  }

  else

  {

  如果是 false 執行這裡;

  };

  三、關於bool的小例子

  (1)

  #include<iostream>

  using namespace std;

  int main()

  {

  bool b =1; //執行此行後,b=1(true)

  if(b)

  cout << "ok!" << endl;

  b = b-1; //執行此行後,b=0(flase)

  if(b)

  cout << "error!" <<endl;

  return 0;

  }

  運行結果:OK!

  (2)

  #include<iostream>

  using namespace std;

  int main()

  {

  bool b =1; //執行此行後,b=1(true)

  if(b)

  cout << "ok!" << endl;

  b = b+1; //執行此行後,b=1(true)

  if(b)

  cout << "error!" <<endl;

  return 0;

  }

  運行結果:OK!

  error!

  若想更瞭解的話,讀者可以在定義b時改成 bool b=0;看看運行結果
來自百度百科:http://baike.baidu.com/view/1557195.html?wtp=tt

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.