小端CPU:高字节保存在高地址,低字节保存在低地址。
int num = 0x12345678(12是高字节,78是低字节)
大端CPU:低字节保存在高地址,高字节保存在低地址。
int num = 0x12345678
英特尔、arm都是小端。
如何判断机器的大小端?
#include <stdio.h>
union node
{
int num;
char ch;
};
void big_small(union node p)
{
p.num = 0x12345678;
if(p.ch == 0x78)
{
printf("It is small-endian.\n");
}
else
{
printf("It is big-endian.\n");
}
}
int main()
{
union node p;
big_small(p);
return 0;
}