Endianness tells us how computers store numbers in memory. It helps us understand how data is arranged when we read or write it.
0
0
Endianness (big-endian vs little-endian) in Embedded C
Introduction
When sending data between different devices that may store numbers differently.
When reading raw data from files or network that use a specific byte order.
When debugging low-level programs that work with memory directly.
When writing code for embedded systems that communicate with hardware.
When converting data formats between systems with different endianness.
Syntax
Embedded C
/* No direct syntax in C, but we check endianness like this: */ #include <stdio.h> int main() { unsigned int x = 1; char *c = (char*)&x; if (*c) { // little-endian } else { // big-endian } return 0; }
Endianness is about byte order in memory, not a language keyword.
We use pointers to check how bytes are stored.
Examples
This prints the first byte of the number in memory. If it prints 0x78, the system is little-endian. If 0x12, big-endian.
Embedded C
unsigned int x = 0x12345678; char *c = (char*)&x; printf("First byte: 0x%x\n", (unsigned char)*c);
This simple check tells the endianness of the system.
Embedded C
unsigned int x = 1; char *c = (char*)&x; if (*c == 1) { printf("Little-endian\n"); } else { printf("Big-endian\n"); }
Sample Program
This program shows how the bytes of a number are stored in memory. It prints each byte and then tells if the system is little-endian or big-endian.
Embedded C
#include <stdio.h> int main() { unsigned int x = 0x12345678; char *c = (char*)&x; printf("Memory bytes of 0x12345678:\n"); for (int i = 0; i < sizeof(x); i++) { printf("Byte %d: 0x%x\n", i, (unsigned char)c[i]); } if (*c == 0x78) { printf("System is little-endian\n"); } else if (*c == 0x12) { printf("System is big-endian\n"); } else { printf("Unknown endianness\n"); } return 0; }
OutputSuccess
Important Notes
Most modern PCs use little-endian format.
Network protocols often use big-endian (called network byte order).
Always check endianness when sharing binary data between different systems.
Summary
Endianness is about the order of bytes in memory for numbers.
Little-endian stores the smallest byte first; big-endian stores the largest byte first.
Knowing endianness helps when working with low-level data or different devices.