Wednesday, May 5, 2010

Testing Endianness Of A Platform

A small code to find the endianness of the current platform for which the code will be compiled & where the executable from the code will be run:

#include

#define BIG_ENDIAN 0
#define LITTLE_ENDIAN 1

int TestByteOrder(void);

int TestByteOrder(void)
{
short int word = 0x0001;
char *byte = (char *) &word;
return (byte[0] ? LITTLE_ENDIAN : BIG_ENDIAN);
}

int main(void)
{
printf("The Endianness of the platform is: %s\n", TestByteOrder() ? "LITTLE Endian" : "BIG Endian");
return 0;
}

No comments:

Post a Comment