C Primer Plus 02 --- integer overflow and formatting output (interesting data storage ring --- this is understandable)

Source: Internet
Author: User

[Cpp] # include <stdio. h> int main (void) {int I = 2147483647; unsigned int j = 4294967295; printf ("% d * % d ** % d \ n", I, I + 1, I + 2); printf ("% u * % u ** % u \ n", I, I + 1, I + 2 ); printf ("% d * % d ** % d \ n", j, j + 1, j + 2 ); printf ("% u * % u ** % u \ n", j, j + 1, j + 2); getchar (); return 0 ;} the running result is as follows: 2147483647 *-2147483648 **-21474836472147483647*2147483648 ** 2147483649-1*0 ** 14294967295*0 ** 1. The knowledge points are described as follows: when an integer exceeds the maximum value he can represent, It loops back and continues from the beginning, just like a ring. Therefore, do not explain the first and fourth printf. What about the second and third printf statements? Yes, it still involves data storage management. You can view my previous post. Of course, you can understand it if you are interested. For the second one, I have nothing to say. Obviously, the positive number range of u is large, so I will always add one. For the third one, it is changed to-1 because the range is exceeded. As for why it is changed to-1, it is also very simple. To help everyone understand, let's first give a normal example of output in the same range. For example, if the range is 0-9, then the number is now 10, so the loop goes over, which is equivalent to 0. In fact, the same rule applies to the output of different data types. For the third output above, why is-1? You can also think that, in fact, it is 4294967295. During computer compilation, it is found that it is not of the % d type and is relatively large, let's just loop a few circles. First, loop a circle. (The whole here refers to the length range that data can represent. For 4294967295, a circle is 4294967296 ), reduced to-1 and found that-1 is in the range of % d, so it is-1. If you do not agree with the result, you can verify it by yourself, I have already verified it. In a word, if there is no error in compiling and running, you need to understand it according to the idea of the computer. He will not make a mistake, because he is only a program. Since it can run, then there must be rules that can be run. What I mentioned above is a rule. Of course, it is just an understanding. In fact, the computer just gets a bit different, and % d gets a low position of 16 1 (Note: My host has a 32-bit system, the int type occupies 32 bytes), and then the highest digit 0 in the low position represents positive, and 1 represents-. Then, in the computer's internal computation, the obtained result is a complement code-1, so the value is-1. (Note: One is unsigned, the other is signed, and the complement value is calculated according to different formulas.) (Note: As I just mentioned, subtract a circle, in fact, we subtract high-level data until it is reduced to 0. In fact, the computer has not been reduced to 0, so we can simply remove it, but we can understand it, which is more convenient) if you are interested, read the previous post for more details. And this post http://bbs.csdn.net/topics/340253678 And this post http://blog.csdn.net/wang6279026/article/details/8114805 This is the case for most of them. You can see it once. Another example: the printf function, regardless of your variable type, just explains the data according to the meaning of the format operator, for example: int I =-1; printf ("% u \ n ", i); The 32-bit platform will output 4294967295, because the-1 source code is 32 1 (on the 32 platform) and the 16-bit output is 65535 pieces of personal understanding. You are welcome to criticize and correct it. You may not be able to switch in some places. Please contact us. 2. format the output [cpp] # include <stdio. h> int main (void) {unsigned int un = 3000000000; short end = 200; long big = 65537; long verybig = 12345678908642; printf ("% u, % d \ n ", un, un); printf (" % hd, % d \ n ", end, end); printf (" % ld, % hd \ n ", big, big); printf ("% lld, % ld \ n", verybig, verybig); getchar (); return 0 ;} this program can also be understood in my own way. Note: h stands for short (16 bits), l stands for long output result: 3000000000,-1294967296200,20065537, 112345678908642,194 28999383. display octal and hexadecimal [cpp] # include <stdio. h> int main (void) {int x = 100; printf ("% d * % o ** % x \ n", x ); printf ("% d * % # o ** % # x \ n", x); getchar (); return 0;} output result: 100*144 ** 64100*0144 ** 0x644. well, let me summarize the essence. The unsigned and signed data are cyclical. However, if the two types do not match the output, it is actually okay, because, the binary number in a computer can be either a signed number or an unsigned number. This is the true core. Finally, let's take a small example: assume that the platform is 8-bit. The unsigned number of 11111111 is 127, and the signed number is-1. Their binary values are the same in the computer.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.