当前位置:网站首页>C语言如何分辨大小端
C语言如何分辨大小端
2022-07-31 05:09:00 【怎么这么多名字都被占了】
震惊!C语言分辨大小端竟如此简单
c语言有个面试题,叫分辨电脑是大端还是小端。
大端模式,是指数据的高字节保存在地址空间的低地址中,而数据的低字节保存在地址空间的高地址中
小端模式,是指数据的高字节保存在地址空间的高地址中,而数据的低字节保存在地址空间的低地址中
怎么分辨?上代码
#include<stdio.h>
int main(void){
short a=0x01;//0x..为16进制数,0x01写成short类型是00000000 00000001
char *c=&a;//这里面访问变量里的地址时,遵循char的访问规则,如果是大端,则*c=0
if((*c)==0){
printf("big\n");
}else{
printf("small\n");
}
return 0;
}???是不是很懵?我相信有点基础的人肯定知道这个
#include <stdio.h>
typedef union Un{
char c;
short i;
}un;
int main()
{
un n;
n.i=1;
if (1 == n.c){
printf("small\n");
}
else{
printf("big\n");
}
return 0;
}这段代码应该不用解释了,网上一大堆。那么,我来解释一下上面的代码。
如注释所说,变量a在地址空间中是这么存的:
00000000 00000001
而指针类型的变量p中存的地址是a的地址,但用char的访问规则读取时,只读取
00000000
或
00000001
的一个。如果读取的是00000000,说明高字节保存在地址空间的低地址中,反之说明低字节保存在地址空间的低地址中。由此,就可以判断出大小端了,是不是很简单?
还有什么方法?分享出来吧!
边栏推荐
- 【ORACLE Explain 详解】
- 分布式事务处理方案大 PK!
- STM32——DMA
- 12 reasons for MySQL slow query
- 面试Redis 高可靠性|主从模式、哨兵模式、Cluster集群模式
- Minesweeper game (written in c language)
- ABC D - Distinct Trio (Number of k-tuples
- 城市内涝及桥洞隧道积水在线监测系统
- Interview | Cheng Li, CTO of Alibaba: Cloud + open source together form a credible foundation for the digital world
- Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构
猜你喜欢

MySQL事务隔离级别详解

Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ

110 MySQL interview questions and answers (continuously updated)

mysql uses on duplicate key update to update data in batches

mysql存储过程

MySQL8.0.26安装配置教程(windows 64位)

Three oj questions on leetcode

MySQL optimization slow log query

CentOS7 —— yum安装mysql

为什么要用Flink,怎么入门使用Flink?
随机推荐
110道 MySQL面试题及答案 (持续更新)
城市内涝及桥洞隧道积水在线监测系统
DVWA安装教程(懂你的不懂·详细)
Unity mobile game performance optimization series: performance tuning for the CPU side
Unity框架设计系列:Unity 如何设计网络框架
【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
ABC D - Distinct Trio (Number of k-tuples
Refinement of the four major collection frameworks: Summary of List core knowledge
ERP Production Operation Control Kingdee
可点击也可直接复制指定内容js
MySQL optimization: from ten seconds to three hundred milliseconds
MySQL优化:从十几秒优化到三百毫秒
MySQL开窗函数
SQL row-column conversion
Centos7 install mysql5.7
MySQL常见面试题汇总(建议收藏!!!)
Temporal线上部署
MySQL database backup
MySQL(更新中)
What are the advantages and disadvantages of Unity shader forge and the built-in shader graph?