The storage mode of various basic data types in the C language in the memory

Integer data:

        All integers (positive and negative zero) exist in the form of complement in memory. For a positive integer, its complement is its original code itself. For a negative integer, its complement is the inverse of the original code plus one.

 

Character data:

        Put the corresponding ASCII code of the character (integer, see the ASCII code table for the mapping relationship) in the storage code unit, and these ASCII code values ​​are also stored in the computer in the form of two's complement. [1]

 

Real data:

       Also called floating point number,  it is also stored in binary format in computer. The key is how to convert decimal number into binary to represent. 

Example: 12.63 

First, the integer part is: 1100 

decimal part: 0.63 2=1.26, the first digit after the decimal is 1,0.26 2=0.52, the second digit of the decimal is 0, 0.52 2=1.04, the third digit is 1, With 0.04 2=0.08, the fourth place is 0, 0.08*2 is the fifth place, and so on, so the final result is 1100.10100001b (the following calculation is omitted).

However, note that the actual storage of floating-point numbers in the C language (including C++/Java) is not the way to directly store "integer binary + decimal binary", this is only the first step. After converting to binary, it needs to be processed. The actual storage standard is IEEE 754. If you are interested, please see:

https://www.cnblogs.com/fengliu-/p/7455246.html

Or you can directly check the IEEE 754 standard.

Combined with C/C++/Java, the actual output is better to see if the binary code matches. (I will update my experimental learning results when I have time)

 

note:

① When encountering floating-point numbers that have been "multiplying uncleanly", the final number of bits that can be taken depends on the allocated bytes of the floating-point type data corresponding to the compiler. The more the number of bytes, the more accurate. Therefore, double is more accurate than float, not only is the upper limit of the integer part higher, but the decimal part can also take a lower number of digits, so it is more precise. Moreover, back to the definition of the base (relationship with the decimal), there are

 1100.1010 = 1 * 2^3 + 1 * 2^2 + 0 * 2^1 + 0 * 2^0 + 1 * 2^-1 +0 * 2^-2 +1 * 2^-3 + 0 * 2^-4

       If the decimal part of a floating-point number cannot be expressed as the sum of a1 1/2 + a2 1/4 + a3*1/8 +… (a can be 1 or 0), such as 0.75, 0,875, it will always be "unclean" , So "simple" decimals such as 0.1, 0.2, 0.3 are stored in computer memory as approximate values, and cannot be represented accurately.

    Therefore, try to avoid large floating-point numbers and small floating-point number operations. Due to the storage characteristics of floating-point numbers, small floating-point numbers are often lost and to determine whether two floating-point numbers or a floating-point number and an integer are equal to each other, use abs (xy )<0.000001 this form.

 

In addition, in an environment that supports Chinese, Chinese is also stored using a mapping table (such as a GBK table) and mapped to a hexadecimal number (actually a binary number).

 

 

Reference materials:

[1]https://www.nowcoder.com/questionTerminal/b8d34c56da1245399200555f88c5877e?orderByHotValue=1&page=1&onlyReference=false

Source: Niuke.com

Integer data:

Guess you like

Origin blog.csdn.net/gaggagaasd/article/details/100604859