Bite Bytes
Bite Bytes

Reputation: 1513

Why BOOL in Windows is of type int?

Why not defining BOOL as an enum like in :

enum BOOL {FALSE, TRUE};

Is there any reason why BOOL must be specified explicitly as int (or any other integral type)?

Upvotes: 3

Views: 1478

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 596287

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 is the oldest one. Its definition is simply

typedef int BOOL;

The C programming language uses "int" as its boolean type, and Windows 1.0 was written back when C was the cool language for systems programming.

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.

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.

Upvotes: 8

tjs
tjs

Reputation: 31

The reason is that C originally had no bool type. As per the C Language Specification, any non-zero integral value evaluates to true and 0 evaluates to false. The BOOL typedef was a later addition by Microsoft for the programming of Windows. The enum would still be evaluated under the covers to 0 and 1 (or something else depending on the machine).

Upvotes: 2

Related Questions