当前位置:网站首页>[Zhang San learns C language] - deeply understand data storage
[Zhang San learns C language] - deeply understand data storage
2022-07-02 06:47:00 【Xiexishan】
【 Zhang Sanxue C Language 】— Deep understanding of data storage
One , data type
Different data types represent the size of space opened up by different data in memory
eg:int In memory 4 Bytes
| whole type | Content Be careful : The contents in brackets are the number of bytes occupied by each type |
|---|---|
| data type | char(1 byte ) short(2 byte ) int(4 byte ) long(8 byte ) |
| floating-point | float (8 byte ) double(16 byte ) |
| Construction type | An array type ( The memory size is decided by yourself ) Structure struct ( Observe memory alignment ) enumeration enum( Memory elements determine ) Joint type union( The largest element ) |
| Pointer types | void Indicates the type , Usually used for the return type of a function , The parameters of the function , Pointer types |
C Language has no string type , So use arrays to store strings eg:char[10]="abcdef"
Two . The storage of integer data in memory
1. Source code , Inverse code , Complement code
The symbols in the computer have been stored in memory in binary form
Data is stored in memory in the form of complement
The first is the sign bit , The remaining bits are data bits
0 Being positive ,1 Negative
Source code : Direct binary can be translated into binary in the form of positive and negative numbers
Inverse code : The sign bits remain the same , The data position is reversed
Complement code : Inverse code +1
Positive numbers : Source code = Inverse code = Complement code
negative : Store... As a complement
eg:
Positive numbers
date:10
bin:00000000 00000000 00000000 00001010
Source code = Inverse code = Complement code =00000000 00000000 00000000 00001010
negative
date:-10
bin: 10000000 00000000 00000000 00001010
Inverse code :11111111 11111111 11111111 11110101
Complement code :11111111 11111111 11111111 11110110
2. The reason why data is stored in the form of complement in memory
a. The use of complement can unify the symbol bit and data field
b.CPU Only adders , Using complement can make addition and subtraction unified
3, data storage
Byte order :
Concept :cpu The order in which data in memory is accessed in bytes
Because there are high and low addresses in memory , There are high and low binary data
The host byte order mainly depends on cpu framework : X86- The small end ; MIPS- Big end
Byte order is mainly used in network communication , Communication between different hosts , If the byte order is different, it will cause data ambiguity
classification :int=0x11223344
Big end storage : The low address stores the high address Storage in memory 11 22 33 44
Small end storage : The low address stores the low address Storage in memory 44 33 22 11
notes : The size end is a high-frequency examination site in interviews and examinations , To learn more, click the link below to learn .
3、 ... and . The storage of floating-point data in memory
1,IEEE754 The standard document stipulates , The representation of any floating-point number in memory is divided into three parts
| Symbol | Memory occupied by each bit of single precision floating-point numbers | Memory occupied by each bit of double precision floating-point numbers |
|---|---|---|
| s— Sign bit | 1 A bit | 1 A bit |
| E— Exponential position | 8 A bit | 11 A bit |
| M— mantissa | 23 A bit | 52 A bit |
Floating point numbers are expressed in exponential form in the computer , Decompose into : Numeral character , mantissa , Exponential sign , Index Four parts .
The number symbol accounts for 1 Bit binary , The positive and negative of a number .0 Express positive ,1 Negative
Exponential operators account for 1 Bit binary , The positive and negative of the index .
The mantissa represents the significant number of floating-point numbers ,0.xxxxxxx, But don't save the beginning 0 Sum point .
The effective number of the index .
How many places does the index occupy , How many bits does the mantissa occupy , It's up to the computer system to decide .

Two special cases :
E For all 0: At this time, the real index value is 1-127=-126,M Represent true value , Usually used to indicate positive and negative 0
E For all 1: At this time, the real index value is 255-127=128,M For all 0 Hour means positive and negative infinity
2. Value range
| type | Number of bits | Significant figures | Range of values |
|---|---|---|---|
| float | 32 | 6-7 | -3.410(-38)~3.410(38) |
| double | 64 | 15-16 | -1.710(-308)~1.710(308) |
| long double | 128 | 18-19 | -1.210(-4932)~1.210(4932) |
If storage is more important than accuracy , Consider using float type . contrary , If accuracy is the most important condition , Then use double type .
Floating point variables can be promoted to larger Radix types ( from float Type to double type ). When you perform arithmetic on floating-point variables , Usually there will be an improvement . This arithmetic is always performed with the same precision as the variable with the highest precision .
float f_short;
double f_long;
long double f_longer;
gf_short = f_short * f_long
In the previous example , Variable f_short Promote to type double And with f_long Multiply ; then , Result rounded to type float, Then give to f_short.
3. Application example analysis
stay C and C++ in , The following assignment statement :
float a=0.1;
Compiler error :warning C4305: ‘initializing’ : truncation from 'const double ’ to 'float ’
reason : stay C/C++ in , To the right of the equal sign 0.1, Think it's a float, But the compiler thinks it's a double( Because decimals default to double), So I have to report this warning, It's usually changed to 0.1f It's all right. .
Welcome to exchange , If it works , Remember to comment + Collection + Pay attention to ! meow ~~
Today's rubbish : I didn't understand until I grew up , Nothing is difficult in the world , I'm just afraid that those who have a mind .
边栏推荐
- 蚂蚁集团g6初探
- Pytest (1) case collection rules
- The table component specifies the concatenation parallel method
- js中map和forEach的用法
- flex九宫格布局
- There is no way to drag the win10 desktop icon (you can select it, open it, delete it, create it, etc., but you can't drag it)
- (the 100th blog) written at the end of the second year of doctor's degree -20200818
- Name six schemes to realize delayed messages at one go
- Kotlin - 验证时间格式是否是 yyyy-MM-dd HH:mm:ss
- Self study table Au
猜你喜欢
随机推荐
Shardingsphere JDBC
自学table au
ModuleNotFoundError: No module named ‘jieba. analyse‘; ‘ jieba‘ is not a package
Sentry搭建和使用
PgSQL learning notes
QQ email cannot receive the email sent by Jenkins using email extension after construction (timestamp or auth...)
FE - Eggjs 结合 Typeorm 出现连接不了数据库
Eslint configuration code auto format
Sentry construction and use
apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
ZZQ的博客目录--更新于20210601
The table component specifies the concatenation parallel method
kali最新更新指南
计算属性普通函数写法 和 set get 写法
Self study table Au
Blog directory of zzq -- updated on 20210601
Redis - cluster data distribution algorithm & hash slot
由于不正常断电导致的unexpected inconsistency;RUN fsck MANUALLY问题已解决
Browser scrolling for more implementations
Linux MySQL 5.6.51 community generic installation tutorial









