写一个程序判断大小端
时间: 2024-03-30 22:08:44 浏览: 58
笔_面试题总结.pdf
以下是一个程序,用于判断本机的大小端存储模式:
```c
#include <stdio.h>
int check_sys() {
int a = 1;
char *p = (char *) &a;
return *p; // 返回1,小端;返回0,大端;
}
int main() {
int ret = check_sys();
if (ret == 1)
printf("小端\n");
else
printf("大端\n");
return 0;
}
```
这个程序通过将一个整型数据1(0x00000001)的地址强制转换为char*类型,然后解引用判断其值是1还是0,如果返回1,则表示是小端存储模式,反之则为大端存储模式。
#### 引用[.reference_title]
- *1* *2* [写一个程序验证系统的大小端存储格式](https://blog.csdn.net/lijunlin0329/article/details/127299079)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [判断机器大小端模式](https://blog.csdn.net/czg13548930186/article/details/78080132)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文