BOOL 与bool

BOOL

 
 
 

一、BOOLbool的区别

  1、类不同
 
  bool为布尔
 
  BOOL为int
 
  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;看看运行结果
posted @ 2013-01-17 15:34  vanishfan  阅读(631)  评论(0)    收藏  举报