Front and back operators

Source: Internet
Author: User

This is the answer from Thunder: [cpp] # include <stdio. h> int main (void) {int a, B; a = 5; a = a + a ++; printf ("a = % d \ n", ); a = 5; B = a + a ++; printf ("B = % d \ n", B); return 0 ;}the output is: disassembly process: [cpp] PUBLIC _ main EXTRN _ printf: NEAR _ data segment $ SG529 DB 'a = % d', 0aH, 00 H $ SG530 DB 'B = % d', 0aH, 00 H _ data ends _ text segment _ a $=-4 _ B $=-8 _ main PROC NEAR; 3: int main (void) {push ebp mov ebp, esp sub esp, 8; 4: int a, B; 5:; 6: a = 5; mov DWORD PTR _ a $ [ebp], 5; Value assignment, a = 5; 7: a = a + (a ++); mov eax, dword ptr _ a $ [ebp] add eax, dword ptr _ a $ [ebp]; a + = a (a = 10) mov dword ptr _ a $ [ebp], eax mov ecx, dword ptr _ a $ [ebp] add ecx, 1; a ++, a = 11 mov dword ptr _ a $ [ebp], ecx; 8: printf ("a = % d \ n", a); mov edx, dword ptr _ a $ [ebp] push edx push offset flat: $ SG529 call _ printf add esp, 8; 9:; 10: a = 5; mov dword ptr _ a $ [ebp], 5; 11: B = a + (a ++); mov eax, DWORD PTR _ a $ [ebp] add eax, dword ptr _ a $ [ebp]; a + = a (a = 10, B = 10) mov dword ptr _ B $ [ebp], eax mov ecx, dword ptr _ a $ [ebp] add ecx, 1; a ++, a = 11, B unchanged mov DWORD PTR _ a $ [ebp], ecx; 12: printf ("B = % d \ n", B); mov edx, dword ptr _ B $ [ebp] push edx push OFFSET FLAT: $ SG530 call _ printf add esp, 8; 13: return 0; xor eax, eax; 14:} mov esp, ebp pop ebp ret 0 _ main ENDP _ text ends end first, it must be clear that: the Operation Order of the expression is 1 from left to right. for a = a + a ++, the operation procedure is: a = 5 + 5 = 10, and then a automatically adds 2. for B = a + a ++, the computation process is: B = 5 + 5 = 10. Then, a then adds itself and then looks at a slightly complex program: [cpp] # include <stdio. h> int main (void) {int a, B; a = 5; a = a ++; printf ("a = % d \ n ", a); a = 5; B = (++ a) + (a ++) + (++) + (a --); printf ("a = % d \ n", a); printf ("B = % d \ n", B); return 0 ;} the calculation result is: Do not paste the ASM code ------------------------------- first of all, it must be clear that the operation order of the expression is from left to right. The statement a = a ++; the execution process is: 1. the result of the first a ++ is 5, and the result of the second a ++ is 52. in this case, the expression returns a = 5 + 5 = 10, and then a executes two self-added statements B = (++ a) + (a ++) + (a ++) + (++ a) + (a --); the execution process is: 1. the first result of ++ a is 62. the results of both a ++ are 63. the result of the first ++ a is 7, and the result of the last ++ a is 84. the result of a -- is 8. The final expression result is B = 6 + 6 + 6 + 7 + 8 + 8 = 41. Then, the value of a is updated, a has a total of two post-auto-increment operations and one post-auto-subtraction operation, so a = a + 2-1 = 9 (because the pre-operation has been performed during expression operation, at the end of the expression operation, the value of a is 8)

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.