Tuesday, December 13, 2016

How to check for endianness on Linux

Endianness is a simple concept, architectures that store the most significant byte (in a multi-byte data type) at the end are big-endian and the ones that store the least significant byte at the end are little endian.

Don't mix bytes and bits here. There's no big or little endian in a char or any single byte data type.

Here's how you check for it on Linux:
#include <stdio.h>

int
main(int argc, char **argv)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
        printf("little endian\n");
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
        printf("big endian\n");
#endif

        return (0);
}

No comments:

Post a Comment