The computer uses storage units to store data (encoded data). variables are used in the C language to identify the storage units in the computer.
The C language contains long I, j; I = 1; j =-1;, then the corresponding computer has four bytes of integer variables, the storage status is 00000000000000000000000000000001 (in hexadecimal format: 00000001) and 11111111111111111111111111111111 (in hexadecimal format: ffffffff ). Therefore, the storage statuses of variables I and j are 00000001 and ffffff. Because the types of variables are fixed, their actual values are also fixed.
The computer uses binary, And the integer, decimal, or character-encoded data are all 01 strings. Different types of data may correspond to the same encoding. Therefore, the storage status on the computer indicates that the data cannot be determined. The value can be determined only when the data type is determined.
In C language, I + j is literally interpreted as 1 +-1 and the value is 0. The actual execution process in the computer is 00000001 + ffffffff to get 00000000, and the result is an integer 0 encoded.
When viewing the output result of a variable, the printf function can decode the storage status of the data to an integer and present it to the user. The decoded data of printf is the output value. The printf function does not consider that the types of related variables only decode data based on their own format characters. Therefore, the value of the variables output by the printf function is not necessarily the actual value of the variables. For example, the output result of the printf ("% ld, % lu \ n", j, j); statement is.
In summary, the storage status of variables is fixed, the actual value is fixed, and there may be multiple output values.
You can use the printf function to output the storage status of integer data. The format character o outputs the storage status of integer data in octal format, while the format character X (x) can output the storage status in hexadecimal format. (The roles of these two characters are often misunderstood .)
The storage status of floating point data is complex and usually not output or analyzed.
The actual values of floating point variables are usually very long and some are invalid numbers, so they are easy to be ignored. The actual value of the floating point variable is different from the original value in the program in most cases. It is of great significance to emphasize its actual value. Use the printf function to output many BITs (such as 20 bits) of floating point data. The final number in the output data will be 0. Therefore, the actual value of floating point data can be considered by removing the data before the last 0. Float fa = 0.1; double fb = 0.1; printf ("%. 20f, %. 20f \ n ", fa, fb);, the program output (VC6.0) is 0.10000000149011612000, 0.10000000000000001000. Therefore, the original value of the variable fa is 0.1, and the actual value is 0.10000000149011612; the original value of the variable fb is 0.1, and the actual value is 0.10000000000000001.
The output value of floating-point data, mostly the result of retaining the decimal places after the actual value.
The original value and actual value of the integer variable also have an error. If short j = 50000 is found, the original value of the variable j is 50000, the actual value is certainly not 50000 (its actual value is only 32767 at the maximum ).
Distinguishing the meanings of these concepts is of great significance for correct use of variables. The variable operations in the program are based on (storage status) actual values.