Hungarian Notation

You may also notice that some of the variables in HELLOWIN.C have peculiar-looking names. One example is lpszCmdParam, passed as a parameter to WinMain.

Many Windows programmers use a variable-naming convention known as Hungarian notation, in honor of the legendary Microsoft programmer Charles Simonyi. Very simply, the variable name begins with a lowercase letter or letters that denote the data type of the variable. For example, the lpsz prefix in lpszCmdParam stands for ”long pointer to a string terminated by zero.“

The h prefix in hInstance and hPrevInstance stands for ”handle“; the n prefix in nCmdShow stands for ”number,“ and usually specifies an integer. Two of the parameters to WndProc also use Hungarian notation: wParam is a WORD and lParam is a LONG.

When naming structure variables, you can use the structure name (or an abbreviation of the structure name) in lowercase as either a prefix to the variable name or as the entire variable name. For example, in the WinMain function in HELLOWIN.C, the msg variable is a structure of the MSG type; wndclass is a structure of the WNDCLASS type. In the WndProc function, ps is a PAINTSTRUCT structure and rect is a RECT structure.

Hungarian notation helps you avoid errors in your code before they turn into bugs. Because the name of a variable describes both the use of a variable and its data type, you are much less inclined to make coding errors involving mismatched data types.

The variable name prefixes I'll be using in this book are shown in the following table:

Prefix Data Type

c char
by BYTE (unsigned char)
n short or int
i int
x, y short (used as x-coordinate or y-coordinate)
cx, cy short (used as x or y length; the c stands for ”count“)
b BOOL (int)
w WORD (unsigned int)
l LONG (long)
dw DWORD (unsigned long)
fn function
s string
sz string terminated by 0 byte