tags:

views:

113

answers:

2

Guys, having quick look in Winnt.h I have discovered that there is a lots of typedefs and one of them is for example CHAR for a char. Why? What was the purpose of these typdefs? Why not use what's already there (char, int etc.)?
Thank you.

+7  A: 

The win32 API needs to be language agnostic. The typedefs are tied to actual item sizes on the x86 processor. Thus CHAR is char, DWORD is unsigned long.... It's all so that languages other than C and C++ can "plug in" to the API even with differing memory models.

Billy ONeal
To answer his specific question, there's no reason for `CHAR` other than symmetry.
Chris Lutz
@Chris Lutz: There's also the semantic difference. CHAR is a Windows API datatype. `char` is a C and C++ specific item. In .Net for example, the standard `char` type is 16 bits wide.
Billy ONeal
@Chris Lutz: Well, theoretically, there's no requirement in the C++ standard that a `char` is 8 bits wide. So if Microsoft were to one day start supporting a platform with, say, 7-bit bytes, their `CHAR` typedef would come in handy.
jalf
@jalf - Not true. The C (and therefore C++) standard requires `unsigned char` to hold values from 0 to 255 and `signed char` to hold values from -127 to +127, therefore the `char` type must be at least 8 bits. A theoretical 7-bit system would either have 14-bit `char`s or not be standard C. Even if it's bigger (say, 9-bit), `char` is defined as the smallest type avaliable to the system, so `typedef`-ing it to a smaller type is impossible.
Chris Lutz
+2  A: 

The WIN32 API needs to be platform agnostic as well. When the compiler adjusts for different word sizes, the types may also change as well.

For example, on 16-Bit platforms:

typedef WORD unsigned int;
typedef DWORD unsigned long;

On 32-bit platforms:

typedef WORD unsigned short;
typedef DWORD unsigned int;

This an example, your mileage may vary.

Thomas Matthews