What is the difference between an unsigned short and a USHORT?

23,309

Solution 1

USHORT is a macro which is not part of the official C++ language (it's probably defined or typedef'ed somewhere). unsigned short is an official type defined by the C++ language as an integer that can at least hold numbers between 0 and 65535.

Use unsigned short and your code will be portable - don't use USHORT unless you company's coding standard requires it.

Solution 2

unsigned short is a standard C++ expression and USHORT is not. The precise definition can be found in the Wikipedia article Integer (computer science).

Sometimes, we typedef unsigned short USHORT in the header. Then USHORT can be used as well as unsigned short.

Share:
23,309
Stas Jaro
Author by

Stas Jaro

C++, C, Objective-C, VHDL, Java, AVR Assembly, HTML, Javascript, PHP

Updated on August 07, 2022

Comments

  • Stas Jaro
    Stas Jaro almost 2 years

    What is the difference between USHORT and an unsigned short and when would you use each?

  • Seth Carnegie
    Seth Carnegie over 12 years
    Note that a typedef is not a macro.
  • Stas Jaro
    Stas Jaro over 12 years
    i was looking at some sample code here:cplusplus.com/forum/lounge/17053 in the first code block. Could you take a look and see if you can figure out why they use it?
  • Cheers and hth. - Alf
    Cheers and hth. - Alf over 12 years
    @stas: it's just a Microsoft'ism, something done without any other reason than that other's doing it. It's about the same as the C'ism of writing void foo(void), when in C++ there is no point to the second void, the one in the argument list. Or the Javaism of writing double getSin(), when in C++ the get` has no purpose and is just visual noise. Cheers,
  • Adrian Cornish
    Adrian Cornish over 12 years
    @stas Looks like Microsoft C++ to me - this is a particular style adopted by the Windows guys and the Microsoft compiler defines those types. If you are working in code like this - it is usually conventional to follow the style of existing code.