大端模式
大端模式,是指数据的低位保存在内存的高地址中,而数据的高位保存在内存的低地址中。
小端模式
小端模式,是指数据的低位保存在内存的低地址中,而数据的高位保存在内存的高地址中。
判断程序
文件:check_endian.c
编译:gcc -o check_endian check_endian.c
#include <stdio.h>
// check system by char pointer
int check_system()
{
int n = 0x04030201;
if (*(char *)&n == 0x01)
{
return 1;
}
else
{
return 0;
}
return 0;
}
// check system by union
int check_system2()
{
union check
{
int i;
char c;
} u;
u.i = 0x04030201;
if (u.c == 0x01)
{
return 1;
}
else
{
return 0;
}
return 0;
}
int main(int argc, char *argv[])
{
// if (check_system() == 1)
if (check_system2() == 1)
{
printf("little endian.\n");
}
else
{
printf("big endian.\n");
}
return 0;
}
备注:
1)check_system和check_system2均可正常用于判断大小端。
2)代码亦可通过下面命令获取:git clone https://github.com/wangfuyu/check_endian