/* How can I determine whether a machine's byte order is big-endian or little-endian? */
#include <stdio.h>
int main(int argc, char const *argv[])
{
/*method 1*/
int x = 1;
if(*(char *)&x == 1)
printf("little-endian\n");
else
printf("big-endian\n");
/*method 2*/
{
union {
int i;
char c[sizeof(int)];
} unx;
unx.i = 1;
if(unx.c[0] == 1)
printf("little-endian\n");
else
printf("big-endian\n");
}
return 0;
}
转自: http://c-faq.com/misc/endiantest.html