Checking whether the underlying architecture is little-endian or big-endian using C

Big-endian and little-endian are two formats to store multibyte data types into a computer’s memory. These two formats are also called network byte order and host byte order respectively. In a multibyte data type such as int or long or any other multibyte data type the right most byte is called the least significant byte and the left most byte is called the most significant byte. In big-endian format, the most significant byte is stored first, and thus gets stored at the smallest address byte, while in a little-endian format the least significant byte is stored first.

As an example, if x a four-byte integer contains a hex value 0x76543210 (‘0x’ stands for hex), the least significant byte will contain 0x10 and the most significant byte will store 0x76. Now if you take a pointer c of type char and assign x‘s address to c by casting x to char pointer, then on the little-endian architecture you will get 0x10 when *c is printed and on the big-endian architecture, you will get 0x76 while printing down *c. Thereby you can find out the endianness for the machine.

int x = 0x76543210;
char *c = (char*) &x;

Big endian format:
------------------
Byte address  | 0x01 | 0x02 | 0x03 | 0x04 | 
              +++++++++++++++++++++++++++++
Byte content  | 0x76 | 0x54 | 0x32 | 0x10 |
			 
Little endian format:
---------------------
Byte address  | 0x01 | 0x02 | 0x03 | 0x04 | 
              +++++++++++++++++++++++++++++
Byte content  | 0x10 | 0x32 | 0x54 | 0x76 |

C program to detect little and big-endian architecture

/* 
   Write a C program to find out if the underlying 
   architecture is little endian or big endian. 
 */
 
#include <stdio.h>
int main ()
{
  unsigned int x = 0x76543210;
  char *c = (char*) &x;
 
  printf ("*c is: 0x%x\n", *c);
  if (*c == 0x10)
  {
    printf ("Underlying architecture is little endian. \n");
  }
  else
  {
     printf ("Underlying architecture is big endian. \n");
  }
 
  return 0;
}

As said earlier, big-endian is also called network byte order, while little-endian is called host byte order. There are a set of functions to convert 16-bit and 32-bit integers to network byte order and vice versa. The htons (host-to-network-short) and htonl (host-to-network-long) functions convert 16-bit and 32-bit values respectively from the host (machine) to network byte order; the ntohs and ntohl functions convert from network to host byte order.

We can also write a small function to determine if an underlying machine architecture is little-endian or big-endian. Function check_for_endianness() returns 1 if the machine is little-endian, 0 otherwise.

C function to check little and big-endian architecture

* 
   Function check_for_endianness() returns 1, if architecture 
   is little endian, 0 in case of big endian.
 */
 
int check_for_endianness()
{
  unsigned int x = 1;
  char *c = (char*) &x;
  return (int)*c;
}

Advantages of Endianness

Both formats, big and little-endian have their own advantages and disadvantages.

In “Little-Endian” form, assembly language instructions for picking up a 1, 2, 4, or longer byte number proceed in exactly the same way for all formats: first, pick up the lowest order byte at offset 0. Also, because of the 1:1 relationship between address offset and byte number (offset 0 is byte 0), multiple precision math routines are correspondingly easy to write.

In the “Big-Endian” form, by having the high-order byte come first, you can always test whether the number is positive or negative by looking at the byte at offset zero. You don’t have to know how long the number is, nor do you have to skip over any bytes to find the byte containing the sign information. The numbers are also stored in the order in which they are printed out, so binary to decimal routines are particularly efficient.

How Relevant Endian Order is?

Endian order means that any time a computer accesses a stream (a network tap, a local file, or an audio, video, or multimedia stream), the computer has to know how the file is constructed. For example, if you write out a graphics file (such as a . BMP file, which is Little-Endian format) on a Big-Endian machine, you must first reverse the byte order of each integer you write, or another “standard” program will not be able to read the file.

Hope you have enjoyed reading the C program to check little and big-endian architecture.