Questions about printf ("%d,%d", I--, i++)

Source: Internet
Author: User

First look at a few situations

1,

	int i=1;
	printf ("%d,%d\n", I--, i++);
The results of the operation are: 2,1

This is related to the compiler and can be clearly seen through the Assembly


The first step: Put the value of I into the buffer [ebp-0e8h]=1;

The second step: I value plus 1,i=i+1=2;

Step three: Put the value of I into the buffer [ebp-0ech]=2;

Fourth step: I value minus 1,i=i-1=1;

Fifth step: Put the buffer [ebp-0e8h]=1, into the stack

Sixth step: Put the buffer [ebp-0ech]=2, into the stack

Therefore, the printout is 2,1

Summarize:

by + + or--the order of operations is from right to left, so first calculate i++,i++ in the calculation process will produce a buffer, the return value is the value of the buffer, both in addition to 1 before the need to back up the buffer address here is [Ebp-0e8h]=1,

After that, i=i+1=2;

Similarly, the buffer of the second expression [ebp-0ech]=2,

After that, i=i-1=1;

Then the first, the return value of the second expression, respectively, into the stack "1,2" so that the output is 2 1;

2,

	int i=1;
	printf ("%d,%d\n", i++,++i);

The first step: I value plus 1,i=i+1=2;

Step two: Put the value of I into the buffer [ebp-0e8h]=2;

The third step: I value plus 1,i=i+1=3;

Fourth step: Put the i=3, into the stack

Fifth step: Put the buffer [ebp-0e8h]=2, into the stack

So, the output is 2, 3.

Summarize:

by + + or--the order of the operations is right-to-left, so the ++i,++i return value is first calculated as I itself, this should be 2, but the next operation is affecting the value of I, in addition, printf output stream cache stack is in all expressions after the calculation of the stack, only need to know first into the stack is the address of I stored in the value,

The following expression i++,i++ in the calculation process will produce a buffer, the return value is the value of the buffer, both in addition to 1 before the need to back up, where the backup address is [ebp-0e8h]=2, after i=i+1=3, the return value of the buffer [ebp-0e8h]=2, The second stack is the return value of the second expression [ebp-0e8h]=2, and the first time into the stack is the first expression of the return value I value (at this time has been changed to 3), so the output stack inside is "3,2", print output 2,3.


Here are a few examples of two variables, and the reason is similar, note that with the ++/--, the return is the value of the buffer, the first is the return of the value of the variable itself, in addition to its own value may be affected by another expression, so, in the absence of calculation, is not know the value of I.

1,

	int x=2,y=3;
	printf ("%d,%d\n", (x + +) +y,++y);//Equivalence x+++y

The first one into the stack is the Y value itself, while the other expressions have no effect on it, so the stack y==4

The second stack is the value of the buffer for the expression, x+y=2+4=6, so the stack 6

Output value is 6 4

The following examples can be analyzed on their own, look at the disassembly immediately understand the meaning, not one to speak

2,

	int x=2,y=3;
	printf ("%d,%d\n", x+y++,++y);

3,

	int x=2,y=3;
	printf ("%d,%d\n", x+y,++y);

4,

	int x=2,y=3;
	printf ("%d,%d\n", x+y,y++);


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.