Delphi has long supported a few basic numeric types and I was wondering how they are related to each other.
In Delphi 2007 I found these declarations (some are confl
The signed one-byte integer type is ShortInt
. You can remember its size by the fact that it's not the same size as usual C implementations of the short
type.
As for capitalization, capitalize the first letter. The documentation tends to leave the "int" part at the end lowercase, as in Longint, but I think it's more common to capitalize it. Don't write the types in all capitals unless you're using Platform SDK types and you want your code to show its C roots; otherwise I'd just write Word
and DWord
, Long
and ULong
, etc.)
Delphi 2009, perhaps earlier, already defines types like Int8 and UInt32. As for how to define Int
and UInt
, I'd say don't. The language you're using already defines Integer
and Cardinal
; don't introduce new type names when you don't have to. Keep the names you already have, and then everyone else will know what you're talking about. (Besides, Int is already a function in the System unit.)
Use Use Cardinal
when you want an unsigned type and don't care about its size; use LongWord
when the variable must be exactly four bytes. Likewise for Integer
and LongInt
.Cardinal
when you want a four-byte unsigned type; use LongWord
when you want a generic unsigned type and don't care about the size. Likewise for Integer
and LongInt
, nowadays. If you're writing 16-bit code, use LongInt
when you need four bytes and use Integer
when you don't care about the size; Cardinal
and LongWord
didn't exist in Delphi's and Turbo Pascal's 16-bit days.
The common wisdom for years had been that Integer
and Cardinal
would become 64-bit types on a 64-bit compiler, but that is apparently not the case. Instead, they will remain 32-bit types, just as their counterparts in Microsoft C++ do. Furthermore, there will be a new type, NativeInt
, which will be a 64-bit type in a 64-bit compiler. The LongInt
and LongWord
types will become 64-bit types because they have always been the same size as the Pointer
type, which was 32 bits even in 16-bit times.