"Go" C-language self-increment decrement operator in-depth analysis

Source: Internet
Author: User

Transferred from: http://bbs.csdn.net/topics/330189207

C language's self-increment + +, self-subtraction-operator has always been a problem for beginners, and even a lot of veteran will be confused, recently I saw a problem on the Internet:
#include <stdio.h>
void Main ()/* Main function */
{
int a,b,c,d;
a=5;
b=5;
C= (a++) + (a++) + (a++);
D= (++b) + (++b) + (++B);
printf ("a=%d,b=%d,c=%d,d=%d\n", a,b,c,d);
}
What was the result?

Then Eric searched and found that a lot of similar problems, that is, the self-increment self-subtraction operator is a common problem, based on this, Eric decided to make a small analysis of the self-increment operator, hoping to give the C language enthusiasts to provide reference to solve the problem of confusion.

Self-increment self-decrement operator syntax

The increment operator + + adds 1 to the operand's value, and its operand must be a variable lvalue (which can be simply understood as a variable). For self-increment is add 1 this point, Eric think everyone will not have any doubt.

The problem is that + + can be placed either in front of the operand or in the back, such as:

++i;
i++;
++i said that I increased by 1 and then participated in other operations, while i++ was I after the operation, the value of I increased from 1.

The self-subtraction operator-similar to that, is only a change to minus, so it is not restated.

Example anatomy

Let's take a look at some examples to understand the properties of the increment operator, the self-subtraction operator, and the self-awareness

Example one:

int i=3;
int j=4;
i++;
++j;
printf ("%d,%d\n", I, j);

Eric would not be confused about this, and the result is 4, 5; Let's make a little change:

int i=3;
int j=4;
int a = i++;
int b = ++j;
printf ("%d,%d\n", A, b);

How much is the result? This starts with the difference between the front and back of the + +, the result is 3, 5. In conjunction with this example, we will come back to understand "+ + pre-set: I increment 1 and then participate in other operations; + + post: I The value of I is increased by 1 since I participate in the operation." Obviously, a = i++, because the assignment operation is performed first, and then the result is a=3,i=4, and b = ++j;
The B,J is 5 because it is self-increasing first and then assigned a value.

In fact, the basic truth is so simple, but in the case of more complex points, what will be, see:

Example two:

int i=3;
int j=4;
int a = i++ + i++;
int b = ++j + ++j;
printf ("%d,%d\n", A, b);

The problem comes again, i++ + i++ is to increase the first time, add, then self-increment, and then assign the value, or add the first assignment and then add two times from it. In addition, how will ++j behave?

The result is: 6,12

It is understood that the original i++ understanding should be performed after the completion of the entire expression of other operations, and then only self-increment, so the example of a=3+3=6, and then I added 2 times, i=5; instead, ++j is the first self-increment and then participate in other operations, so b=6+6=12.

Is it completely clear that this is the right thing to do? Then go back to the question in the intro:

Example three:

int i=3;
int j=4;
int a = i++ + i++ + i++;
int b = ++j + ++j + ++j;
printf ("%d,%d\n", A, b);

One might say, this is very simple, I fully understand: a=3+3+3=9,i=6,b=5+5+5=15,j=5. Is that really the case?

And the result is: 9,19

This is a very, very confused. For a = i++ + i++ + i++; We have no doubt, + + after the execution of the entire expression after the other operation, and then the self-increment, the above example has also been verified, but B = ++j + ++j + ++j;

In addition to the priority of the budget law itself, there is a binding problem in the principle expression. In ++j + ++j + ++j, because there are two peers of the + operation, according to the left binding of the + operator, at compile time, in fact, the first process (++j + ++j) This part, and then add this result again and ++j. See Assembly code for specific procedures:

int b = ++j + ++j + ++j;
0040B7DD mov ecx,dword ptr [ebp-8]
0040B7E0 Add ecx,1
0040B7E3 mov dword ptr [EBP-8],ECX//First ++j
0040B7E6 mov edx,dword ptr [ebp-8]
0040b7e9 Add edx,1
0040B7EC mov dword ptr [Ebp-8],edx//Second ++j
0040B7EF mov eax,dword ptr [ebp-8]
0040B7F2 add Eax,dword ptr [ebp-8]//++j + ++j
0040B7F5 mov ecx,dword ptr [ebp-8]
0040b7f8 Add ecx,1
0040B7FB mov dword ptr [EBP-8],ECX//Third ++j
0040b7fe add Eax,dword ptr [ebp-8]//++j + ++j + ++j
0040b801 mov dword ptr [EBP-10H],EAX//assignment to B

In addition we look at a = i++ + i++ + i++, the assembly code:

int a = i++ + i++ + i++;
0040B7B6 mov eax,dword ptr [ebp-4]
0040B7B9 add Eax,dword ptr [ebp-4]//I+i
0040B7BC add Eax,dword ptr [ebp-4]//I+i+i
0040B7BF mov dword ptr [EBP-0CH],EAX//assignment to a
0040B7C2 mov ecx,dword ptr [ebp-4]
0040B7C5 Add ecx,1
0040B7C8 mov dword ptr [EBP-4],ECX//First time i++
0040B7CB mov edx,dword ptr [ebp-4]
0040b7ce Add edx,1
0040B7D1 mov dword ptr [EBP-4],EDX//second time i++
0040B7D4 mov eax,dword ptr [ebp-4]
0040b7d7 Add eax,1
0040B7DA mov dword ptr [EBP-4],EAX//third time i++

As expected. In this situation, the problem of the pre-placement of the + + operator should be completely resolved.

To verify the above conclusions, we look at:

Example four:

int i=1;
int j=1;
int a = i++ + i++ + i++ + i++ + i++ + i++ + i++; Seven
int b = ++j + ++j + ++j + ++j + ++j + ++j + ++j;
printf ("%d,%d\n", A, b);
printf ("%d,%d\n", I, j);

Rule is the rule, our computer is not the matrix of the matrix, always follow its

A = 1+1+1+1+1+1+1 = 7, i=8
b = 3+3+4+5+6+7+8 = J=8

All OK, congratulations. You're still living in the 21st century Earth, don't worry matrix control your thinking and life

Note: The above results and explanations from the VC compiler, but for + + This problem is related to the compiler, different manufacturers may understand the inconsistency, because there is no other development environment at hand, can not do a comprehensive analysis, this article is just to illustrate some of the characteristics of the ++,--operator, especially the difference between the front and back of the problem. If the problem is confused, it is best to write the program to do the test, do not mechanically. Thank you! In practical programming practice, in addition to trying to figure out a similar problem, Eric believes that the introduction of environment-related programming techniques should be avoided.

"Go" C-language self-increment decrement operator in-depth analysis

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.