1 #include <stdio.h> 2 3 union 4 { 5 short s; 6 char c[sizeof(short)]; 7 }un; 8 9 int main(int argc, char *argv[]) 10 { 11 un.s = 0x0102; 12 13 if(sizeof(short) == 2) 14 { 15 if(un.c[0] == 0x01 && un.c[1] == 0x02) 16 { 17 printf("big-endian\n"); /* 高位存低字节,低位存高字节 */ 18 } 19 else if (un.c[0] == 0x02 && un.c[1] == 0x01) 20 { 21 printf("little-endian\n"); /* 低位存低字节,高位存高字节 */ 22 } 23 else 24 { 25 printf("Unknow"); 26 } 27 } 28 else 29 { 30 printf("sizeof(short) == %d\n", sizeof(short)); 31 } 32 33 return 0; 34 }
时间: 2024-10-07 17:12:09