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

Source: Internet
Author: User
1. for integer overflow, directly add the code
#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 X-2147483648 X-2147483647
2147483647*2147483648 ** 2147483649
-1*0 ** 1
4294967295*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. As follows:

For the second one, I have nothing to say. Obviously, the positive number range of U is relatively large, so I will always add one.

For the third one, it is changed to-1 because the range is exceeded. It is also very easy to figure out why it is changed to-1,

To help you understand, let's first give a normal example of output in the same range. For example, if the range is 0-9, then my current number is 10, and 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. This post is also published.

Another example:

The printf function only interprets data according to the meaning of the format operator regardless of your variable type.
For example:
Int I =-1;
Printf ("% u \ n", I );
The 32-bit Platform outputs 4294967295, because the source code of-1 is 32 1 (on 32 platforms)
16-bit output 65535

For personal understanding, we welcome your criticism and correction. You may not be able to switch in some places. Please contact us.

2. format the output
#include<stdio.h>int main(void){unsigned int un = 3000000000;short end = 200;long big = 65537;long 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 indicates short (16 bits), L indicates long. The output result is: 3000000000,-1294967296.
200,200
65537,1
12345678908642,194 28999383. Display octal and hexadecimal

#include<stdio.h>int main(void){int x = 100;printf("%d*%o**%x\n",x,x,x);printf("%d*%#o**%#x\n",x,x,x);getchar();return 0;}
Output: 100*144 ** 64
100*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.

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.