Fundamentals 4 min read

Why Does Endianness Matter? Visualizing Big‑Endian vs Little‑Endian in C

This article explains the concepts of big‑endian and little‑endian byte ordering, shows memory layout examples with 0x1234, provides C code to display byte order in memory, and discusses how pointer casting behaves differently on each architecture.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Why Does Endianness Matter? Visualizing Big‑Endian vs Little‑Endian in C

Big‑Endian

In big‑endian order the most significant byte of a multi‑byte value is stored at the lowest memory address, and the remaining bytes follow in decreasing significance. For a 16‑bit integer 0x1234 the memory layout is:

Address1: 0x12
Address2: 0x34

This mirrors the way we write numbers, with the highest digit first.

Little‑Endian

In little‑endian order the least significant byte is stored at the lowest address, with subsequent bytes increasing in significance. The same 0x1234 value is stored as:

Address1: 0x34
Address2: 0x12

Demonstration Code

The following C program prints the byte representation of an integer, allowing you to see the actual layout in memory.

#include <stdio.h>
void show_memory(char *start, int n){
    int i;
    for (i = 0; i < n; i++)
        printf(" %.2x", &start[i]);
    printf("
");
    for (i = 0; i < n; i++)
        printf(" %.2x", start[i]);
    printf("
");
}
int main(){
    int i = 0x44332211;
    show_memory((char *)&i, sizeof(i));
    return 0;
}

Running this program on a little‑endian machine prints the bytes in reverse order compared with a big‑endian machine.

Pointer Arithmetic Example

A second example adds one to the byte pointer before casting back to an int pointer, illustrating how the order of bytes affects the value read through the pointer.

#include <stdio.h>
int main(){
    int i = 0x44332211;
    int *p = (int *)((char *)&i + 1);
    for(int j = 0; j < 10; j++) {
        printf("%.2x ", *((char *)&i + j));
    }
    printf("
%p 0x%x
", &i, *p);
    return 0;
}

Key Takeaway

A computer does not distinguish “high” or “low” bytes; it simply reads memory sequentially. On a big‑endian system the first byte read is the most significant, while on a little‑endian system it is the least significant. Understanding this behavior is essential for correct pointer casting and binary data handling.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

memory layoutendiannessbig-endianlittle-endian
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.