Explaining Endianness in C
Explaining Endianness in C
I am on a journey to finish C Programming and Memory Management — Full Course. To follow, copy the code and paste it into an Online C compiler on pythontutor.com. The name of the journey is CTJ. These are my public notes. This is the step CTJ-53; The aim is to introduce the concepts of little and big endians.
1. Endianness is the order in which bytes are stored in memory
- coined by Danny Cohen in an Internet Experiment Note, 1980 [1]
- borrowed from Gulliver’s Travels (Jonathan Swift, 1726)
- in Lilliput, two factions war over which end of a boiled egg to crack
- Big-Endians crack the big end, Little-Endians the small end
- Cohen used it to mock the arbitrariness of the byte-order choice
2. C knows how to handle this automatically based on what architecture you’re on
- you can check with python
[embed]
3. There are two most common endian formats: big and little endian
- there is also middle-endian (mixed-endian) where bytes in neither big nor little order
- famous example: PDP-11 — stored 32-bit values as two 16-bit words big-endian, but bytes within each word little-endian
- bi-endian — architectures that can switch at runtime (ARM v3+, PowerPC, SPARC v9, RISC-V)
4. Big endian stores the most significant byte first at the lowest address
- the concept of {most,least}_siginifcant is straight from positional notation in math[2]
- most significant = highest weight: removing it changes the value the most
- e.g. in
1234:1is most significant digit (removing it loses 1000),4is least significant digit (removing it loses 4) - same for bytes: in
0x12345678,0x12is the most significant byte - e.g. given
0x12345678value in hex - 1 hex digit = 4 bits (a nibble); 2 hex digits = 1 byte
- why hex? because 4 bits → ²⁴ = 16 values → maps perfectly to 16 hex symbols (0–9, A-F) as hex is base16 and its values then aligns with bytes even though not perfectly (it’s 2 hex for 1 byte)
- if you wanted 1 hex to 1 byte, 8 bits → ²⁸ = 256 values → would need 256 symbols, unworkable for humans
- so we split the byte into two nibbles: two hex digits per byte
0x12345678= 8 hex digits = 4 bytes — byte boundaries are immediately visible- decimal
305419896is the same number — byte boundaries invisible - anyways, in Big endian you’d store it as you defined it
[embed]
- the most siginifcant byte (0x12) is stored at the lowest memory address
- TJ: there is a whole culture war in the endiann sphere about this
5. Little endian stores the least significant byte first at the lowest address
- 1978 — Intel 8086 released and it was little-endian
- 1981 — IBM PC standardized on x86, sealed the win
- not technical superiority — pure market dominance
- the least signifcant byte is stored first, see the opposite direction
[embed]
6. Let’s write some C
- let’s make a 4-byte integer variable (in hex so 2 values are always 1 byte)
[embed]
- then, let’s make a single-byte character pointer by creating an an array through reference
[embed]
- running
preturnsxV4\x12because the numerical values are decoded into ASCII[3]
[embed]
- to print the lowest address in decimal, i.e.
120
[embed]
6. links
메타데이터
- post_id
- 74084f2a094b
- slug
- explaining-endianness-in-c-74084f2a094b
- url
- https://medium.com/@pavolkutaj/explaining-endianness-in-c-74084f2a094b
- canonical_url
- https://medium.com/@pavolkutaj/explaining-endianness-in-c-74084f2a094b
- author_url
- https://medium.com/@pavolkutaj
- status
- ok
- fetched_at
- 2026-07-11 12:46:10