Search code examples
cwindowsendianness

Endianness for all c data types on Windows OS


I am trying to document the different data types and how they are stored in memory in C. I know how many bytes each data type takes up but I would like to know how the endianness of every data type. This is specifically for Windows.


Solution

  • Endianness is (usually) a function of the hardware, not the OS or the language, so all multi-byte types should have the same endianness.

    Emphasis on should.

    For x86 and x86_64 (on which Windows primarily runs), all multi-byte types are little-endian.

    But there are always going to be some oddball platforms. The DEC VAX was little-endian except for floating-point types, which were stored in a combined big- and little-endian order. From Kapps & Stafford1:

    The VAX was designed in part to be compatible with the PDP-11 computer. The PDP-11 is a 16-bit machine, and 32-bit and 64-bit floating point numbers were stored as sequences of 16-bit words with the most significant part coming first. This was unfortunate for the VAX, because the VAX almost universally places the least significant part first. Floating-point numbers are the main exception to this rule. As a consequence, when an F_floating number is stored in a longword, we have to reverse the first 16 bits with the last 16 bits.

    IOW, each 16-bit word was big-endian (byte order 01), but the sequence of 16-bit words was little-endian, so the byte order of a 32-bit F_float was 2301.

    As for type sizes...

    C does not specify sizes for the "traditional" scalar types like int, long, float, double, etc. It specifies a minimum range of values that each type must be able to represent. A char must be able to represent all characters in the basic execution character set, meaning it must be at least 8 bits wide, but it may be wider (9-bit bytes and 36-bit words are a thing, or at least used to be). An int must be able to represent values in at least the range -32767..32767, meaning it must be at least 16 bits wide.


    1. Kapps, Charles A. and Robert L. Stafford, VAX Assembly Language and Architecture, Prindle, Weber & Schmidt 1985