这里使用联合和指针两种方法
#includevoid is_lsb(void); void is_lsb1(void); int main() { is_lsb(); is_lsb1(); return 0; } void is_lsb(void) { union u_data { unsigned char a; unsigned int b; }data; data.b=0x12345678; printf("data size is %ldn",sizeof(data)); printf("b=%xn",data.b); printf("a=%xn",data.a); if(data.a==0x78) { printf("the os is LSBn"); } else if(data.a==0x12) { printf("the os is MSBn"); } else { printf("wrongn"); } } /使用指针来判断大端字节序还是小段字节序/ void is_lsb1(void) { unsigned int a=0x12345678; unsigned char *p; p=(unsigned char *)&a; printf("*p=%xn",*p); if(*p==0x78) { printf("os is lsbn"); } else if(*p==0x12) { printf("os is msbn"); } else { printf("wrongn"); } }
输出结果



