bool与BOOL的区别

来源:百度文库 编辑:神马文学网 时间:2024/10/06 20:40:55

一、BOOL和bool的区别

  1、类型不同  BOOL为int型  bool为布尔型  2、长度不同  bool只有一个字节  BOOL长度视实际环境来定,一般可认为是4个字节  3、取值不同  bool取值false和true,是0和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  using namespace std;  int main()  {  bool b = -1; //执行此行后,b=1(ture)  if(b)  cout << "ok!" << endl;  b = b-1; //执行此行后,b=0(flase)  if(b)  cout << "error!" <  using namespace std;  int main()  {  bool b = -1; //执行此行后,b=1(ture)  if(b)  cout << "ok!" << endl;  b = b+1; //执行此行后,b=1(ture)  if(b)  cout << "error!" <