您的位置:首页 > 其它

BOOL与bool区别

2009-10-14 13:40 405 查看
1、先查看定义:

 

BOOL是微软定义的宏,实际上是int型占4个字节,该定义位于VC目录下WINDEF.H、AFX.H,可以找到如下代码:

// WINDEF.H
typedef unsigned long       DWORD;
typedef int                 BOOL;
typedef unsigned char       BYTE;
typedef unsigned short      WORD;
typedef float               FLOAT;

// AFX.H
#define FALSE   0
#define TRUE    1
#define NULL    0


 

bool是C/C++关键字,关于MSDN2005帮助文档中,解释如下:

 

This keyword is a built-in type. A variable of this type can have values true and false. Conditional expressions have the type bool and so have values of type bool. For example,
i!=0
now has true or false depending on the value of
i
.

The values true and false have the following relationship:

 

When a postfix or prefix ++ operator is applied to a variable of type bool, the variable is set to true. The postfix or prefix -- operator cannot be applied to a variable of this type.

 

The bool type participates in integral promotions. An r-value of type bool can be converted to an r-value of type int, with false becoming zero and true becoming one. As a distinct type, bool participates in overload resolution.

 

2、定义BOOL与bool原因(TRUE FALSE 与 true false)

 

在查阅很多资料后,请教了这方面朋友侦探,下面是他的解释如下:

Koma 12:15:48
C语言中已经有true和false还有null,微软为什么还定义TRUE, FALSE, NULL?
侦探 12:19:42
因为true,false, null内存空间占用1
而TRUE, FALSE, NULL内存空间占用4
侦探 12:20:45
根据intel CPU的内存分页机制,4字节可以防止内存松散,防止产生更多的内存碎片,有利于数据的传输


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  null 微软 float 文档 语言 c