When learning C programming, it’s important to understand how computers store data at the hardware level. One concept that often comes up is endianness, which refers to the order in which bytes are arranged in memory. A machine can either be little-endian, where the least significant byte is stored first, or big-endian, where the most significant byte comes first. Knowing the endianness of a machine is crucial for tasks like low-level programming, networking, file handling, and communication between different systems.

with hands-on learning.
get the skills and confidence to land your next move.
Understanding endianness also helps beginners grasp how memory works and why programs behave differently on different architectures. For instance, when sending data over a network or reading binary files, misinterpreting byte order can lead to incorrect results. In this article, we’ll explore simple C programs to check the endianness of your machine and explain the underlying concept in an easy-to-understand way.
Program 1: Check Endianness Using Pointer Typecasting
This program demonstrates a simple method to check if your machine is little-endian or big-endian using pointers.
#include <stdio.h>
int main() {
unsigned int num = 1;
char *ptr = (char*)#
if(*ptr == 1) {
printf("Machine is Little Endian.\n");
} else {
printf("Machine is Big Endian.\n");
}
return 0;
}
In this program, we declare an integer num
with the value 1
and then typecast its address to a char*
. Since a char
occupies one byte, we can inspect the first byte of the integer. If the first byte is 1
, the least significant byte comes first, indicating a little-endian machine. Otherwise, it is big-endian. This approach is straightforward and helps beginners visualize how data is stored in memory.
Program 2: Check Endianness Using Union
This program demonstrates another method to determine endianness using a union
.
#include <stdio.h>
union {
unsigned int num;
unsigned char bytes[4];
} test;
int main() {
test.num = 1;
if(test.bytes[0] == 1) {
printf("Machine is Little Endian.\n");
} else {
printf("Machine is Big Endian.\n");
}
return 0;
}
Here, we use a union to store the same data as an integer and as a byte array simultaneously. By inspecting the first element of the byte array, we can determine the byte order. This method is particularly useful because unions allow access to the same memory in different ways, helping beginners understand memory layout and representation of data types.
Program 3: Check Endianness Using Bit Shifting
This program shows an alternative method using bitwise operations.
#include <stdio.h>
int main() {
unsigned int num = 0x1;
if(*(char*)&num == 0x1) {
printf("Machine is Little Endian.\n");
} else {
printf("Machine is Big Endian.\n");
}
return 0;
}
This method is similar to pointer typecasting but emphasizes that we are checking the least significant byte directly. Beginners can see how bit manipulation and memory addresses work together to determine endianness. Understanding this method is helpful when working with binary protocols or performing low-level data processing.
Visualizing Endianness
To understand endianness more clearly, let’s look at an example. Suppose we have a 4-byte integer with the hexadecimal value 0x12345678
. The way it is stored in memory differs depending on the machine’s endianness.
Little-Endian (Least Significant Byte First):
Memory Address | 0x00 | 0x01 | 0x02 | 0x03 |
---|---|---|---|---|
Stored Value | 0x78 | 0x56 | 0x34 | 0x12 |
In little-endian format, the least significant byte 0x78
is stored at the lowest memory address, and the most significant byte 0x12
is stored at the highest address. This is why pointer typecasting works when we check the first byte.
Big-Endian (Most Significant Byte First):
Memory Address | 0x00 | 0x01 | 0x02 | 0x03 |
---|---|---|---|---|
Stored Value | 0x12 | 0x34 | 0x56 | 0x78 |
In big-endian format, the most significant byte 0x12
is stored first, at the lowest memory address, and the least significant byte 0x78
is stored last. This is commonly used in network protocols, sometimes referred to as network byte order.
This visualization helps beginners see why the first byte is checked in the programs. By examining the first memory location, you can determine the endianness of any machine quickly.
Frequently Asked Questions (FAQ)
Here are some common questions about endianness and how to check it in C.
Q1. What is little-endian and big-endian?
Little-endian stores the least significant byte first, while big-endian stores the most significant byte first.
Q2. Why does endianness matter in programming?
It matters when sharing binary data between different machines, working with network protocols, or reading/writing binary files.
Q3. Are most modern machines little-endian or big-endian?
Most modern machines, including Intel and AMD processors, are little-endian.
Q4. Can a program change the endianness of a machine?
No, endianness is determined by the hardware architecture, but programs can convert data to a different byte order when needed.
Q5. Which method is easiest for beginners to check endianness?
Using a simple pointer typecasting method is the easiest and most intuitive.
Conclusion
Checking the endianness of a machine is an essential skill for understanding how computers store and interpret data. Whether you use pointer typecasting, unions, or bitwise operations, each method provides insight into memory organization and byte order. By practicing these programs, beginners can develop a deeper understanding of low-level programming concepts, which is useful for tasks like network programming, file handling, and cross-platform development.
Additional & References
For beginners who want to explore endianness and memory representation further, these resources are very useful:
- C Standard Library Documentation – Explanation of unions, pointers, and memory management.
- Programiz C Tutorials – Beginner-friendly tutorials with hands-on examples for practicing arithmetic and geometry programs.
- GeeksforGeeks C Programming Section – Detailed explanations, sample programs, and exercises for reinforcing core concepts.
- Learn-C.org – Interactive platform to write and test C programs online, ideal for experimenting with volume calculations.