C program to find out the endianess of the machine/processor

Endianess specifies the way how multiple bytes are stored in the memory. There are two types:Little Endian and Big Endian.

Little Endian: In little endian, Least significant byte is stored first of the multiple data bytes;

For example, if you are trying to store 0x12345678,then 0x78 is stored first.

Big Endian: In big endian, Most significant byte is stored first of the multiple data bytes.

For example, if you are trying to store 0x12345678, then 0x12 is stored first.

We will see different ways of finding endianess of your machine:

Method 1:

int main()
{
      unsigned int i = 1;
      char *ch = (char  *)&i;
      if (*ch)
            printf("Machine Order:Little Endian\n");
      else
            printf("Machine Order:Big Endian\n");
      return 0;
}


Method 2:

int main()
{
     union{
                   int i;
                   char ch[4];
     }endian={0x12345678};

     if (endian.ch[0] == 0x78)
          printf("Machine Order:Little Endian\n");
      else
            printf("Machine Order:Big Endian\n");
      return 0;
}



Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig