为什么 Windows 中的 BOOL 是 int 类型? [英] Why BOOL in Windows is of type int?
问题描述
为什么不将 BOOL
定义为 enum
就像在:
Why not defining BOOL
as an enum
like in :
enum BOOL {FALSE, TRUE};
BOOL
是否有任何理由必须显式指定为 int
(或任何其他整数类型)?
Is there any reason why BOOL
must be specified explicitly as int
(or any other integral type)?
推荐答案
来自 Raymond Chen 关于此主题的博客文章:
From Raymond Chen's blog article on this topic:
BOOL vs. VARIANT_BOOL vs. BOOLEAN vs. bool
还有更多的表达方式.为什么这么多?
Still more ways of saying the same thing. Why so many?
因为每个都是由不同的人在不同的时间发明来解决不同的问题.
Because each was invented by different people at different times to solve different problems.
BOOL
是最古老的.它的定义很简单
BOOL
is the oldest one. Its definition is simply
typedef int BOOL;
C 编程语言使用int"作为其布尔类型,当 C 是系统编程的酷语言时,Windows 1.0 被写回了.
时至今日,绝大多数 Win32 API 仍然是为 C 设计的,因此它可以兼容多种与 C 兼容的编程语言.
The vast majority of the Win32 API is still designed for C to this day, so that it is compatible with a large variety of programming languages that are compatible with C.
枚举在编译器之间存在可移植性问题,与字节大小和位表示的差异有关.此外,enum
直到 1989 年的 ANSI C(又名 C98)才被添加到 C 中,这是在 Windows 的三个版本(1985 年的 1.0、1987 年的 2.0 和 1988 年的 2.1)和枚举之后无论如何,实际上等效于整数.
Enums have portability issues across compilers, related to differences in byte size and bit representation. Besides, enum
wasn't added to C until ANSI C in 1989 (aka C98), which was after three releases of Windows (1.0 in 1985, 2.0 in 1987, and 2.1 in 1988), and enums are effectively equivalent to integers anyway.
这篇关于为什么 Windows 中的 BOOL 是 int 类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!