Thread: what is the WORD datatype?

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    3

    Question what is the WORD datatype?

    I had a programming class assignment to make a simple guessing game ( we're just starting out programming in C) and I read this code that changes the color of the console:
    Code:
    HANDLE mainwin = GetStdHandle ( STD_OUTPUT_HANDLE );
        WORD DefaultColor; 
        CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
        GetConsoleScreenBufferInfo(mainwin, &csbiInfo); 
        DefaultColor = csbiInfo.wAttributes;
    I know what each bit does, but I would like to know what the WORD datatype / structure is? ( sorry if I'm not using the correct terminology, I'm just beginning programming, and it's really fun )

    Thanks alot for the help

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    WORD in a Windows environment is just that - a word. Now the definition of a word is some type of data that is 16 bits (2 bytes) for x86. Typically an alias for short (ONLY guaranteed under Windows!).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    Oh ok, thanks
    So word is sort of like a string that can only hold to characters? (2 bytes?)
    Thanks for the help

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    No, a WORD is just a 2-Byte (unsigned) data type, you can use it for whatever you like.

    Windows Data Types (Windows)

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When we speak of types such as word, dword, qword, etc, they all refer to a storage unit, or place, where we can store at most n bytes (2, 4, 8 in this case respectively). What exactly you want to store in them is up to you, because to the hardware it's all bits.
    Now, these types are all (usually) represented by integer types (short, int, long long respectively in this case).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    Oh ok, its starting to make more sense now,
    Thanks alot Elysia and DeadPlanet,
    + the link was really helpful as well

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Short question: Why are data types bigger that BYTE called *WORD? (WORD, DWORD, QWORD ... )
    Devoted my life to programming...

  8. #8
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Short question: Why are data types bigger that BYTE called *WORD? (WORD, DWORD, QWORD ... )
    A word is the data size that a processor naturally handles. So for a 32-bit processor, it's theoretically 32 bits (or an int), although x86 processors support 16 and 32 bits equally via the *x and e*x registers.

    Since smaller data sizes have to be padded for operations there's really no speed gain from using e.g. bytes vs. words. So it's probably more convenient to define data types that are word size rather than byte size - that way you have a much larger int range at no cost of speed.
    Therefore Windows has DWORD (double word) and QWORD (quad word), which correspond to 2 words and 4 words respectively (or a 16-bit long int and long long int).

    Since the modern Windows API really came about in Windows '95 and that was a 16-bit system, WORD was defined to be a 16-bit data structure (on 16-bit processors, an int, and a short was 8 bits like a char). Hence the 16-bit Windows word. And it stuck on into win32, probably for compatibility reasons.

    EDIT: Nevermind the '95 part, 16-bit started out with DOS, but the point is still valid.
    Last edited by bernt; 07-06-2010 at 08:23 AM.
    Consider this post signed

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I see...
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM