一、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