方法一:共用体
#include <stdio.h>
union Byte
{unsigned int a;char c;
};
int main(int argc, const char *argv[])
{union Byte endianness;endianness.a=0x87654321;printf("endianness.c=%#x\n",endianness.c); if(0x21 == endianness.c)printf("这是一个小端\n");else if(0x87 == endianness.c)printf("这是一个大端\n");return 0;
}
方法二:指针
#include <stdio.h>
int main(int argc, const char *argv[])
{unsigned int a = 0x87654321;char *pa = (char*)&a;if(0x21 == *pa)printf("这是一个小端\n");else if(0x87 == *pa)printf("这是一个大端\n");return 0;
}