What is the point WORD type in C?
Asked Answered
D

3

26

In going through some source code, I found a method in a C program that takes an arguments of the types WORD, DWORD, and PWORD. I know they translate to unsigned numbers, but why are they called WORD?

Dismissive answered 31/5, 2013 at 18:9 Comment(2)
I'd just like to add that when developing for Windows then you can find the definition of the types in this MSDN document. Please note that a WORD is no longer the actual size of a machine word for recent CPUs, but is kept at 16-bit for compatibility.Fire
@Fire I actually found this myself in the source code shortly after posting the question. It was in a header file called WinDef.h. Contained all the typedefs for these types. thank you.Dismissive
S
40

Word Size and Data Types

A word is the amount of data that a machine can process at one time. This fits into the document analogy that includes characters (usually eight bits) and pages (many words, often 4 or 8KB worth) as other measurements of data. A word is an integer number of bytes for example, one, two, four, or eight. When someone talks about the "n-bits" of a machine, they are generally talking about the machine's word size. For example, when people say the Pentium is a 32-bit chip, they are referring to its word size, which is 32 bits, or four bytes.

The size of a processor's general-purpose registers (GPR's) is equal to its word size. The widths of the components in a given architecture for example, the memory bus are usually at least as wide as the word size. Typically, at least in the architectures that Linux supports, the memory address space is equal to the word size[2]. Consequently, the size of a pointer is equal to the word size. Additionally, the size of the C type long is equal to the word size, whereas the size of the int type is sometimes less than that of the word size. For example, the Alpha has a 64-bit word size. Consequently, registers, pointers, and the long type are 64 bits in length. The int type, however, is 32 bits long. The Alpha can access and manipulate 64 bits, one word at a time.

Further read : http://www.makelinux.com/books/lkd2/ch19lev1sec2

Smitt answered 31/5, 2013 at 18:11 Comment(1)
It seems in assembly, word is typically 2 bytes. And another wield thing from my view is, in GDB, for command x, w indicate 4 bytes, and h indicate 2 bytes. It seems not unified.Spatz
H
4

WORD in Windows APIs means 2 bytes.

It was originally used to refer to the pointer-size (as in a CPU with a word length of 16 bits).
The Windows APIs used it in typedefs back in (and before) Windows 3.1 (which only supported 16-bit machines), so the meaning cannot change anymore.

Histolysis answered 31/5, 2013 at 18:10 Comment(3)
in the same way that long float is an older term for doubles in c++?Dismissive
@Nurgle In c++, to print out, or scan for a double, you use "%lf" which stands for long float. Why is that the case if they do not relate?Dismissive
I think your first sentence is a bit misleading. A word is not defined as 2 bytes but as the width of the bus/register. The WORD type on Windows is 16-bit because it was originally developed for 16-bit machines.Fire
F
2

WORD is probably from some older code and usually meant 16-bits, while DWORD usually means 32-bits. If you are unsure you should check your code though, because they have to be defined somewhere.

WORD stems from machine word which size was hardware dependent.

Farrel answered 31/5, 2013 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.