If you want to test little / big endian, you can use union:
int isBigEndian (void) { union foo { size_t i; char cp[sizeof(size_t)]; } u; ui = 1; return *u.cp != 1; }
This works because in a small endian it will look 01 00 ... 00, but in a large endian it will be 00 ... 00 01 (... consists of zeros). Therefore, if the first byte is 0, the test returns true. Otherwise, it returns false. Beware, however, that there are also mixed end machines that store data in different ways (some can switch with precision, others just store data in different ways). PDP-11 saved the 32-bit int as two 16-bit words, except that the word order was canceled (for example, 0x01234567 - 4567 0123).
source share