++ Operator differences in C/C ++ and C #/Java

Source: Internet
Author: User

++ Operator

The increment operator (++) Increments its operand by 1. The increment operator can appear before or after its operand
Incremental operators (++) Add the operand to 1. Incremental operators can appear before or after operands.

The first form is a prefix increment operation. The result of the operation is the value of the operand after it has been incremented.
The first form is the prefix incremental operation. The result of this operation is the value after the operand is added with 1.

The second form is a postfix increment operation. The result of the operation is the value of the operand before it has been incremented.
The second form is the suffix incremental operation. The result of this operation is the value before the operand is increased.

The above is the definition of the ++ operator in msdn. This definition is consistent in the form of C/C ++, C #, and Java (hereinafter referred to as them, therefore, I ++ and I have the same performance in the compiler of the corresponding language.
The difference in the title "differences between ++ operators in C/C ++, C #, and Java" is not the difference in the language itself, but the difference in the corresponding compiler.

I ++ and I are the same in their compilers, But I = I ++; And I = ++ I; I = ++ I has no difference, but I = I ++ is in C/C ++.

I = I ++; the value before I auto-increment is the value of this expression. After the expression is executed, I auto-increment 1. If I = 1, I = I ++ is equal to 1, and I = I ++ is executed after I = 2.

Int I = 1; printf ("% d \ n", I = I ++); printf ("% d \ n", I ); it will print out 1 and 2, that is, it will assign a value to I first, and then add 1 to I.

In C # and Java, I = I ++ is equal to the I value, while the I value will change but it is still the previous value. Because the C # and Java compilers strictly follow the operator priority, evaluate = to the right, that is, the value of I,

Then, add the I value to the stack (add 1 to the stack), and then execute the I first Auto-increment 1 to 2 for the sake of priority ++ before =, then assign the value of 1 in the stack to I, And then I changes back to 1.

Compared with the C/C ++ compiler, the C # and Java compilers are easier to explain and more reasonable.CodeC/C ++ compiler strictly follows the priority,

Therefore, I guess the IP address is imported into the stack instead of directly putting the value into the stack, so there is an inconsistency, but after reading it, it turns out that my guess is wrong, the C/C ++ compiler places values in registers,

However, unlike C # and Java compilers, they do not strictly abide by the operator priority. As far as I know, this is within the scope of C/C ++ compiler undefined,

Their execution logic varies with the compiler. SoDo not go to C/C ++ProgramOtherwise, it will die badly.

See the following code.

Int J, I = 3 ;
J = (I + 3 ) * ++ I;
Printf ( " % D \ n " , I );
Printf ( " % D \ n " , J );

Calculate the result first. The result is 4,28. Are you correct? In C # and Java, It is 4, 24.

Compilation code

Int J, I = 3 ;

MoV Dword ptr _ I $ [EBP], 3 // Store 3 to _ I $ [EBP]

J = (I + 3 ) * ++ I ;

MoV Eax, dword ptr _ I $ [EBP] // put the value of _ I $ [EBP] into the register eax
Add Eax, 1 // Eax + 1 Put the result in eax
MoV Dword ptr _ I $ [EBP], put the eax // eax value in _ I $ [EBP]
MoV ECX, dword ptr _ I $ [EBP] // put the value of _ I $ [EBP] into the register ECx
Add ECX, 3 // ECx + 3 The result is stored in ECx.
Imul ECX, dword ptr _ I $ [EBP] // ECx * _ I $ [EBP]
MoV Dword ptr _ j $ [EBP], ECx // put the ECX value in _ j $ [EBP]

Generated Il

[ 2 ] Int32 J ,[ 3 ] Int32 I

Int J, I =3;

Il_000e: LDC. i4.3 // Add 3 to the stack element 3
Il_000f: Stloc.3 // Pop-up operations on the top elements of the stack and store them to the corresponding underlying variable without elements in the stack

J = (I + 3 ) * ++ I;

Il_0010: Ldloc.3 // Perform a value operation on the corresponding underlying variable and merge it into element 3 of the stack.
Il_0011: LDC. i4.3 // Add 3 elements to stack 3
Il_0012: Add // Pop up and add the two elements at the top of the stack. The result is that element 6 is added to the stack.
Il_0013: Ldloc.3 // Perform value operations on the corresponding underlying variables, and merge them into element 6, 3 of the stack.
Il_0014: LDC. i4.1 // Add 1 to stack elements 6, 3, 1
Il_0015: Add // Pop up and add the two elements at the top of the stack in sequence.
Il_0016: DUP // Copy the top elements of the stack into elements 6, 4, and 4 of the stack.
Il_0017: Stloc.3 // Create a pop-up operation on the top of the stack and store the elements 6, 4 in the stack in the corresponding underlying variable.
Il_0018: Mul // Pop up and multiply the two elements at the top of the stack. The result is that the elements in the stack are 24.
Il_0019: Stloc.2 // Pop-up operations on the top elements of the stack and store them to the corresponding underlying variable without elements in the stack

No related Java bytecode is provided here, but since both JVM and CLR are stack-based VMS, the specific commands are different. The operation logic and method of the commands are the same, see the Il section.

Change J = (I + 3) * ++ I; To J = ++ I * (I + 3. The corresponding assembly code is not changed, but the Il is very different. The output result is.

Related Il

Il_000e: LDC. i4.3  
Il_000f: Stloc.3  
Il_0010: Ldloc.3  
Il_0011: LDC. i4.1  
Il_0012: Add  
Il_0013: DUP  
Il_0014: Stloc.3  
Il_0015: Ldloc.3  
Il_0016: LDC. i4.3  
Il_0017: Add  
Il_0018: Mul  
Il_0019: Stloc.2  

I believe that you can use the previous il annotations to calculate the Il operation process.

 

Finally, the question "int Index = 1; Count = 10; execute Index = (index ++) % count; why is the output result 1"

Why is the post-output result 1 the output index or 1?

Related Il

[ 0 ] Int32 Index ,[ 1 ] Int32 Count

Il_0001: LDC. i4.1  
Il_0002: Stloc.0  
Il_0003: LDC. i4.s 10  
Il_0005: Stloc.1  
Il_0006: Ldloc.0  
Il_0007: DUP  
Il_0008: LDC. i4.1  
Il_0009: Add  
Il_000a: Stloc.0  
Il_000b: Ldloc.1  
Il_000c: Rem  
Il_000d: Stloc.0  

Related assembly code

Int Index = 1 , Count = 10 ;

MoV Dword ptr _ index $ [EBP], 1  
MoV Dword ptr _ count $ [EBP], 10 ; 0000000ah

Index = (index ++) % count ;

MoV Eax, dword ptr _ index $ [EBP]
CDQ  
Idiv Dword ptr _ count $ [EBP]
MoV Dword ptr _ index $ [EBP], EDX
MoV Eax, dword ptr _ index $ [EBP]
Add Eax, 1  
MoV Dword ptr _ index $ [EBP], eax

Through the above Il, can you answer his question?

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.