Super detailed explanation of C language integer and floating point data storage


We all know that the C language has many data types, such as char, int, double, etc. In this blog, let’s sort out and classify these data types. First of all, we can divide the data types into two types, integer and floating point (also Real type) two categories, some readers may be confused, is char not a character type? Why is it not listed? Don't worry, continue reading, the explanation will be given below.

Integer

The integer type contains:
char — the size is 1 byte
[signed] char
unsigned char
short — the size is 2 bytes
[signed] short [int]
unsigned short [int]
int — the size is 4 bytes
[signed] int
unsigned int
long — The special size is 4/8 bytes. The specific size is related to the compiler.
[signed] long
unsigned long [int]
long long — The size is 8 bytes
[signed] long long
unsigned long long [int]

Unsigned represents an unsigned number, and the highest bit also represents data, that is, all bits are valid data bits. The content in square brackets can be omitted, and the omission has no effect.
Why put the character type here, because the C language uses ASCII encoding, and each character corresponds to an ASCII code value, and each ASCII code value corresponds to an integer value (the ASCII code is posted below Table, the blogger found it on Baidu), so the storage form of the character type is actually the same as the integer type. For example, a and 97 (the ASCII code value of a is 97), look at the picture below, so the blogger will The char (character type) is put into the integer type. Can you understand it now?Insert picture description here

Insert picture description here
So what exactly is the storage form of integer data? The answer is stored in the form of two's complement. Binary must be known to everyone, so what is the complement? So why does the concept of complement code appear?
The computer stores data in the form of complement, and stipulates that the original, invert, and complement of positive integers are the same, and the relationship between the original, invert, and complement of negative integers is that all bits of the original code except the sign bit are inverted as the inverted code. The code plus 1 is the complement code. The inversion means that 0 changes to 1, and 1 changes to 0 in binary. A simple example is the binary representation of 10 and -10. Take int as an example:

00000000000000000000000000001010 — Original code (10)
00000000000000000000000000001010 — Inverse code
00000000000000000000000000001010 — Complement code

10000000000000000000000000001010 — original code (-10)
11111111111111111111111111110101 — inverse code
11111111111111111111111111110110 — complement code

The concept of complement code appears because the sign bit and value field can be processed uniformly by using complement code; at the same time, addition and subtraction can also be processed uniformly (CPU only has an adder), and interested friends can learn about it on Baidu.
When viewing the data in the memory, it is displayed in hexadecimal, and the compiler used by the blogger is vs2019, which belongs to the little- endian storage mode (simply understood as storing the data backwards), so it is actually seen in the memory 10 should be 0a 00 00 00 instead of the directly translated 00 00 00 0a, and -10 should be f6 ff ff ff The
Insert picture description here
picture above is from "C Traps and Defects" which introduces the storage mode
. Nonsense, now we verify what we have said on the code, we
Insert picture description here
Insert picture description here
Insert picture description here
can see that the result is the same as we think, so the integer is stored in the form of two's complement, and the rest of the forms can be tested by themselves. Next, we Talk about floating point.

Floating point (real type)

Floating-point types include
float — single-precision size is 4 bytes
double — double-precision size is 8 bytes
long double — long double-precision floating-point type size is 8 bytes

So what is the difference between the storage form of floating point and integer? There is no concept of one's complement in floating-point type.
According to the international standard IEEE (Institute of Electrical and Electronic Engineering) 754, any binary floating-point number V can be expressed in the following form:
V=(-1)^S * M * 2 ^E
(-1)^s represents the sign, when s=0, V is a positive number; when s=1, V is a negative number.
M represents a significant number, which is greater than or equal to 1, and less than 2.
2^E represents the exponent bit.

Insert picture description here
Insert picture description here
When M is put into the memory, the first integer 1 will be concealed, and only the decimal part will be stored. Because M always starts with an integer 1, just add the first 1 when taking it out. This will not cause any misunderstanding, but can increase it. One precision, why not do it?
The E is an unsigned integer , that is to say, E can only be a positive number, but in actual applications, the exponent has to be negative. In order to express the positive and negative, a concept called the middle number appears, which is stored in E When you need to add the intermediate number, and then store it in the memory, IEEE 754 stipulates that the intermediate number of double precision is 1023, and the intermediate number of single precision is 127. Take float as an example, for example:
Insert picture description here
E has two special cases, When the exponent of a number is all 1 or all 0, and all of them are 1, then E is actually 128. The 128th power of 2 is undoubtedly a very large number, and because M is a number between 1 and 2. Number, then we can simply understand that this number is infinite, when all 0, the actual value of E should be -127, 2 to the -127 power is undoubtedly a number very close to 0, so this number can be directly The decimal part is taken out as its own value, and there is no need to add the initial 1. Both the positive and the negative depend on S. The IEEE 754 also stipulates the Insert picture description here
above picture. The above picture is intercepted by the blogger from the C language Chinese network for readers to refer to. The link is attached to the poke I go to the original text

Written at the end

At this point, I must read and think carefully about the storage of integers and floating-point types. So many data types correspond to many format control symbols, and each format symbol corresponds to one or more types. If you accidentally make a mistake, there will be unexpected operating results. There is no need to panic at this time. We can slowly debug our code and look for the problem line by line. This blog will stop here. It's over, I hope to be helpful to the friends. Because of my limited level, there are inevitably some mistakes or unclear expressions in the article. Welcome friends to leave a message in the comment area or send a private message to me to point out, our next blog see.

Guess you like

Origin blog.csdn.net/JunFengYiHan/article/details/114462556