- 大端:高位内存存储低序字节
- 小端:高位内存存储高序字节
- short a = 0x0102,其中 01 高序字节, 02 低序字节
#include<stdio.h>
int main() {
union {
short s;
char c[sizeof(short)];
} un;
un.s = 0x0102;
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");
} else {
printf("Unknown\n");
}
} else {
printf("sizeof(short) == %d\n", sizeof(short));
}
exit(0);
}
我的公众号: