标签:直接 print tty little its author void def date
//csapp 2.58
#include <stdio.h>
#include <inttypes.h>
#include <limits.h>
typedef unsigned char* char_point;
/**
* 判断机器类型,大端机返回0, 小端机返回1,其他返回-1
* author :
* date : 2020-5-7 20:54:03
*/
int is_little_endian(void)
{
int32_t data = 0;
//转换为字符指针,按字节修改内存数据
char_point char_data_point = (char_point) &data;
//只有小端机,为了验证正确性,所以修改高位数据,进行判断,也可以直接修改低位数据判断。
//0x80 : 1000 0000
*(char_data_point + 3) = 0x80;
int result = -1;
if(INT_MIN == data) {
result = 1;
} else if(0x80 == data) {
result = 0;
} else {
printf("data %d\r\n", data);
result = -1;
}
return result;
}
void main(void)
{
int result = is_little_endian();
if(1 == result) {
printf("little endian\r\n");
} else if (0 == result) {
printf("big endian\r\n");
} else if(-1 == result) {
printf("not support\r\n");
} else {
printf("soft error");
}
}
标签:直接 print tty little its author void def date
原文地址:https://www.cnblogs.com/LonelyTraveler/p/12846134.html