Q: How can I determine whether a machine's byte order is big-endian or little-endian?
A: The usual techniques are to use a pointer:
int x = 1; if(*(char *)&x == 1) printf("little-endian\n"); else printf("big-endian\n");or a union:
union { int i; char c[sizeof(int)]; } x; x.i = 1; if(x.c[0] == 1) printf("little-endian\n"); else printf("big-endian\n");
(Note that there are also byte order possibilities beyond simple big-endian and little-endian[footnote] .)
See also questions 10.16 and 20.9b.
References:
H&S Sec. 6.1.2 pp. 163-4