Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of uppercase VOID macro & INT typedef in winnt.h

Anyone any clue as to why there is an uppercase VOID macro defined in the winnt.h header?

To make matters more confusing, VOID is a macro, whereas CHAR, SHORT, INT, and LONG are typedefs.

See the relevant excerpt from winnt.h:

#ifndef VOID
#define VOID void
typedef char CHAR;
typedef short SHORT;
typedef long LONG;
#if !defined(MIDL_PASS)
typedef int INT;
#endif
#endif

A historical reason perhaps for doing VOID* pointer instead of void* pointer?

EDIT: What is even more troubling is to see people using VOID instead of void when doing Windows programming today. You can also see it as part of MSDN docs, e.g. http://msdn.microsoft.com/en-us/library/bb205867(v=vs.85).aspx

like image 669
Nick Avatar asked Jul 12 '11 18:07

Nick


2 Answers

The Windows API is old. Really old. Older than the official C standard old.

This means that, at the start, the Windows API had to deal with all kinds of ancient C compilers, with different levels of language support. Some might not support void. Some might have an int type that's not compatible with what windows thought was int. Some might not understand short. As a workaround, the Windows API provides upper-case portable equivalents that are aliased to whatever works for that particular compiler.

Of course, with modern compilers, things have settled down quite a bit. Everyone supports void, for example. However, in order to maintain compatibility with old code that uses these upper-case macros, the #defines and typedefs have to remain.

like image 161
bdonlan Avatar answered Nov 03 '22 21:11

bdonlan


The original reason is that the Win32 API was originally supposed to be language independent, so they made up their own names (following their naming convention), and then provided a C implementation of that. In reality, much of what they defined tends to be ignored by most people using other languages (and largely even by people using C, for that matter).

As far as why VOID is a #define instead of a typedef, that's pretty simple: the required typedef would be typedef void VOID;, but C (as "defined" by Microsoft's C compilers from that time) simply doesn't allow that, so they use a macro instead.

like image 29
Jerry Coffin Avatar answered Nov 03 '22 20:11

Jerry Coffin