当前位置:网站首页>[深度剖析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️⃣总结
以上便是有关数据在内存中的储存方式的说明
如有不当之处,望指正!
边栏推荐
- Stack implementation calculator
- 程序员养生宝典
- 图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
- Connect timed out of database connection
- [getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order
- 如何招到适合自己店铺的淘宝主播
- php laravel微信支付
- shardingSphere
- P4 installation bmv2 detailed tutorial
- 使用 setoolkit 伪造站点窃取用户信息
猜你喜欢

shardingSphere
![[introduction] approximate value](/img/6b/597178d848dd21110f36601fc31092.png)
[introduction] approximate value

【入门】取近似值

Adding color blocks to Seaborn clustermap matrix

CPU design practice - Chapter 4 practical tasks - simple CPU reference design and debugging

Principle and process of embossing
![[getting started] extract non repeating integers](/img/88/3e96df88e980bd98ac112b18a8678c.png)
[getting started] extract non repeating integers

0 basic introduction to single chip microcomputer: how to use digital multimeter and precautions

Aardio - Shadow Gradient Text

Burpsuite -- brute force cracking of intruder
随机推荐
Aardio - Shadow Gradient Text
【力扣10天SQL入门】Day9 控制流
On several key issues of digital transformation
empirical study and case study
Airsim radar camera fusion to generate color point cloud
Leetcode T40: 组合总和II
Transaction method call @transactional
shardingSphere
【入门】提取不重复的整数
Uni hot update
Insufficient executors to build thread pool
Leetcode T39: 组合总和
PHP laravel wechat payment
web254
On June 30, 2022, the record of provincial competition + national competition of Bluebridge
Chinese font Gan: zi2zi
The difference between interceptors and filters
手工挖XSS漏洞
Leetcode t39: combined sum
Contenttype comparison of all types