当前位置:网站首页>[深度剖析C语言] —— 数据在内存中的存储
[深度剖析C语言] —— 数据在内存中的存储
2022-07-01 08:12:00 【Shark-s】
前言
在前面的博客中,我们已经介绍过了基本的内置类型,具体如下
char //字符数据类型
short //短整型
int //整型
long //长整型
long long //更长的整型
float //单精度浮点数
double //双精度浮点型
今天我们将在这个基础上对这些内置类型进行分析,让大家对其有更加深入的理解
1️⃣数据类型介绍
1.整型家族
char
unsigned char
signed char
short
unsigned short [int]
signed short [int]
int
unsigned int
signed int
long
unsigned long [int]
signed long [int]
其中char也算是整型家族的,因为char在内存中是以ASCII值得形式去存储的,可以看做是一种特殊的整型。
2.浮点数家族🧡
float
double
3.构造类型
数组类型
结构体类型 struct
枚举类型 enum
联合类型 union
4.指针类型
int pi;
char pc;
float pf;
void pv;
2️⃣数据在内存中的存储
1.整型在内存中的存储🤎
在知道整形怎么存储之前,我们先引入:原码、反码、补码。
计算机中的符号数有三种表示方法,即原码、反码和补码。三种方法均有符号位和数值位两部分。
原码:直接将二进制按照正负数的形式翻译成二进制就可以了。
反码:将原码的符号位不变,其他位依次按位取反就可以得到了。
补码:反码 +1 就得到补码。
正数的原码、反码、补码相同。
对整形来说:数据存放内存中其实存放的是补码。
这样也是有原因的:
使用补码,可以将符号位和数值域统一处理;
加法和减法也可以统一处理(CPU只有加法器);
补码与原码相互转换,其运算过程是相同的,不需要额外的硬件电路。
2.浮点型在内存中的存储
根据国际标准IEEE 754,任意一个二进制浮点数V可以表示成下面的形式:
· (-1)^S * M * 2^E。· (-1)^S 表示符号位,当S == 0, V为正数;当S == 1, V为负数。
· M表示有效数字,大于等于1,小于2、
· 2^E表示指数位。
IEEE 754规定:对于32位浮点数,最高的1位是符号位S, 接着的8位是指数E,剩下的23位为有效数字M。
E的两种特殊取值:
E全为0:
当E全为0的时候,即2的次方为0 - 127 为2^-127次方,所以,当s = 0时,一个正数的2^-127次方,是一个从数轴的右边无线趋近于0的数字;而当s = 1时,一个负数的2^-127次方是从数轴左边无线趋近于0的数字。
所以当E全为0的时候,实际就表示的是±0,所以浮点数不可以在程序中出现 与0去比较(浮点数 == 0),而是要跟一段范围去比较。
E全为1:
当E全为1的时候(如果M全为0),即2的次方255 - 127 = 128,所以当S = 0时,表示1 * 2^128次方,当s = 1时表示-1*2^128次方。
所以当E全为1时,其实表示的就是这个浮点数的取值范围。
3️⃣总结
以上便是有关数据在内存中的储存方式的说明
如有不当之处,望指正!
边栏推荐
- XX attack - reflective XSS attack hijacking user browser
- P4 安装bmv2 详细教程
- Anddroid 文本合成语音TTS实现
- Hijacking a user's browser with beef
- On June 30, 2022, the record of provincial competition + national competition of Bluebridge
- OJ input and output exercise
- Access report realizes subtotal function
- Why are some Wills made by husband and wife invalid
- Cmake I two ways to compile source files
- [question brushing] character statistics [0]
猜你喜欢

Gdip - hatchBrush图案表

图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证

Learn the knowledge you need to know about the communication protocol I2C bus

CPU设计实战-第四章实践任务一简单CPU参考设计调试

使用beef劫持用戶瀏覽器

华为机试真题专栏订阅指引

使用beef劫持用户浏览器

The Windows C disk is full

Aardio - Shadow Gradient Text

postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
随机推荐
Provincial selection + noi Part II string
Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization
Data analysis notes 11
On June 30, 2022, the record of provincial competition + national competition of Bluebridge
Leetcode T34: 在排序数组中查找元素的第一个和最后一个位置
window c盘满了
empirical study and case study
uni 热更新
[dynamic planning] p1020 missile interception (variant of the longest increasing subsequence)
web254
Set up file server Minio for quick use
Provincial election + noi Part III tree problems
Why are some Wills made by husband and wife invalid
0 basic introduction to single chip microcomputer: how to use digital multimeter and precautions
使用 setoolkit 伪造站点窃取用户信息
Count number of rows per group and add result to original data frame
Yolov5进阶之六目标追踪环境搭建
使用threejs简单Web3D效果
[question brushing] character statistics [0]
Provincial election + noi Part VII computational geometry