当前位置:网站首页>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,说明高字节保存在地址空间的低地址中,反之说明低字节保存在地址空间的低地址中。由此,就可以判断出大小端了,是不是很简单?
还有什么方法?分享出来吧!
边栏推荐
- 12 reasons for MySQL slow query
- MySQL_关于JSON数据的查询
- [Introduction to MySQL 8 to Mastery] Basics - silent installation of MySQL on Linux system, cross-version upgrade
- Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work
- 2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。
- 有了MVC,为什么还要DDD?
- MySQL database backup
- MySQL忘记密码怎么办
- Blockbuster | foundation for platinum, gold, silver gave nameboards donors
- Workflow番外篇
猜你喜欢

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

STM32——DMA
![Unity Tutorial: URP Rendering Pipeline Practical Tutorial Series [1]](/img/7c/c9ab32bbf43b933e5f84f0d142f7bd.jpg)
Unity Tutorial: URP Rendering Pipeline Practical Tutorial Series [1]

ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your

STM32 - DMA

On-line monitoring system for urban waterlogging and water accumulation in bridges and tunnels

Mysql应用安装后找不到my.ini文件

Lua,ILRuntime, HybridCLR(wolong)/huatuo hot update comparative analysis

Unity Framework Design Series: How Unity Designs Network Frameworks

MySQL transaction (transaction) (this is enough..)
随机推荐
为什么要用Flink,怎么入门使用Flink?
关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
DVWA安装教程(懂你的不懂·详细)
MySQL-如何分库分表?一看就懂
Mysql——字符串函数
Doris学习笔记之监控
ABC D - Distinct Trio(k元组的个数
Mysql应用安装后找不到my.ini文件
MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
docker安装postgresSQL和设置自定义数据目录
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
DVWA靶场环境搭建
CentOS7 - yum install mysql
A complete introduction to JSqlParse of Sql parsing and conversion
Minio upload file ssl certificate is not trusted
[mysql improves query efficiency] Mysql database query is slow to solve the problem
可点击也可直接复制指定内容js
MySQL(更新中)
[debug highlights] Expected input batch_size (1) to match target batch_size (0)
城市内涝及桥洞隧道积水在线监测系统