术语“小端”和“大端”表示多字节值的哪一端(大端或小端)存储在改值的起始地址。
仿造书上page65页的例子,写的代码如下:
-
#include<stdio.h>
-
#include<stdlib.h>
-
int main(int argc,char **argv)
-
{
-
union{
-
short s;
-
char c[sizeof(short)];
-
}un;
-
-
-
un.s=0x0102;
-
//printf("%s",CPU_VENDOR_OS);
-
if(sizeof(short)==2)
-
{
-
if(un.c[0]==1 && un.c[1]==2)
-
printf("big_endian\n");
-
else if(un.c[0]==2 && un.c[1]==1)
-
{
-
printf("little_endian\n");
-
printf("sizeof(short)=%d\n",sizeof(short));
-
}
-
else
-
printf("unkown\n");
-
}
-
else
-
{
-
printf("sizeof(short)=%d\n",sizeof(short));
-
}
-
-
exit(0);
- }
运行结果现实我的机器上是小端字节序,short占的字节为2.