How to Check the Byte Order (Endianness) of Your System in C

There are two basic kinds of computer architectures: little-endian and big-endian. These two terms refer to the byte order used for numerical data. In little-endian systems (which include most CISC architectures), the lower-order bytes are stored first (meaning in the lower addresses). In big-endian systems (which include most RISC architectures), the higher-order bytes are stored first. The terms originated with the classic novel Gulliver’s Travels, in which the people of the nation of Lilliput were hotly divided over whether to break an egg from the little end or the big end. Partisans of the former ideology were called Little-Endians while partisans of the latter ideology were called Big-Endians. The idea is that there was a raging controversy over something that really didn’t matter, which parallels the IRL little-endian vs. big-endian debate in the hacker community.

There’s a fairly simple way to check the endianness or byte order of your system in C. This involves basically creating a two-byte unsigned integer and then dissecting it and examining each of its bytes directly through a pointer to see which is which. I wrote two versions of my program for illustration purposes. The first one is a debug version for examining the values of the bytes, the second is a more refined version that actually tells you whether your system is little-endian or big-endian.

Debug Version:


 1 #include <stdio.h>
 2 #include <stdint.h>
 3 
 4 int mainint argc, char **argv ){
 5         uint16_t word = 0xff00;
 6         char *byte1 = (char *) &word;
 7         char *byte2 = byte1 + 1;
 8         printf"byte1=%x\nbyte2=%x\n", *byte1 & 0xff, *byte2 & 0xff );
 9         return 0;
10 }

The output of this program on my system (80x86 architecture):


byte1=0
byte2=ff

Endianness Version:


 1 #include <stdio.h>
 2 #include <stdint.h>
 3 
 4 int mainint argc, char **argv ){
 5         uint16_t word = 0xff00;
 6         char *byte1 = (char *) &word;
 7         if( *byte1 ) printf"This system is big-endian.\n" );
 8         else printf"This system is little-endian.\n" );
 9         return 0;
10 }

This program’s output:


This system is little-endian.

Basically what I’ve done here is I’ve created a 16-bit unsigned integer where the higher-order byte is all ones and the lower-order byte is all zeros. We know which byte is higher-order and which byte is lower-order because one will evaluate to true while the other will evaluate to false. Next we create pointers to the first and second bytes and check them to see which one is the lower-order byte and which one is the higher-order byte. If the first byte evaluates to true, we know it’s the higher-order byte and the system is big-endian. If the first byte evaluates to false, we know that it’s the lower-order byte and the system is little-endian.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s