当前位置:网站首页>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,说明高字节保存在地址空间的低地址中,反之说明低字节保存在地址空间的低地址中。由此,就可以判断出大小端了,是不是很简单?
还有什么方法?分享出来吧!
边栏推荐
- MySQL_关于JSON数据的查询
- 城市内涝及桥洞隧道积水在线监测系统
- [Detailed explanation of ORACLE Explain]
- Input length must be multiple of 8 when decrypting with padded cipher
- 1. 获取数据-requests.get()
- 分布式事务——分布式事务简介、分布式事务框架 Seata(AT模式、Tcc模式、Tcc Vs AT)、分布式事务—MQ
- CentOS7 install MySQL graphic detailed tutorial
- 再见了繁琐的Excel,掌握数据分析处理技术就靠它了
- STM32——DMA
- [mysql improves query efficiency] Mysql database query is slow to solve the problem
猜你喜欢

sql语句-如何以一个表中的数据为条件据查询另一个表中的数据

Three oj questions on leetcode

sql statement - how to query data in another table based on the data in one table

MySQL8--Windows下使用压缩包安装的方法

Go language study notes - dealing with timeout problems - Context usage | Go language from scratch

mysql存储过程

The Vue project connects to the MySQL database through node and implements addition, deletion, modification and query operations
![[debug highlights] Expected input batch_size (1) to match target batch_size (0)](/img/b3/ff6ccc3cd307befad3bd07a9f4a956.png)
[debug highlights] Expected input batch_size (1) to match target batch_size (0)
![[mysql improves query efficiency] Mysql database query is slow to solve the problem](/img/fa/502a2efdd37508f15541558851a254.png)
[mysql improves query efficiency] Mysql database query is slow to solve the problem

Create componentized development based on ILRuntime hot update
随机推荐
【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
The monitoring of Doris study notes
分布式事务处理方案大 PK!
可点击也可直接复制指定内容js
DVWA靶场环境搭建
面试官问我TCP三次握手和四次挥手,我真的是
Centos7 install mysql5.7
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
CentOS7 install MySQL graphic detailed tutorial
MySQL优化:从十几秒优化到三百毫秒
Sql解析转换之JSqlParse完整介绍
MYSQL下载及安装完整教程
关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
MySQL忘记密码怎么办
MySQL常见面试题汇总(建议收藏!!!)
SQL row-column conversion
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)
Unity mobile game performance optimization series: performance tuning for the CPU side
The 15th day of the special assault version of the sword offer
[mysql improves query efficiency] Mysql database query is slow to solve the problem