++ I and I ++ output

Source: Internet
Author: User
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(int argc, const char *argv[]){int x, y;x = y = 0;printf("%d %d %d\n", ++x, ++x, ++x);printf("%d %d %d\n", y--, ++y, y);return 0;}

Output result: 3 3 3

1 0 0


In vs: for printf output, right-to-left calculation is performed from the output expression, right-to-left calculation is performed, and then the stack is output.

Code in the Assembly in:

<pre class="cpp" name="code">     5: int main(int argc, const char *argv[])     6: {013B13D0 55                   push        ebp  013B13D1 8B EC                mov         ebp,esp  013B13D3 81 EC DC 00 00 00    sub         esp,0DCh  013B13D9 53                   push        ebx  013B13DA 56                   push        esi  013B13DB 57                   push        edi  013B13DC 8D BD 24 FF FF FF    lea         edi,[ebp-0DCh]  013B13E2 B9 37 00 00 00       mov         ecx,37h  013B13E7 B8 CC CC CC CC       mov         eax,0CCCCCCCCh  013B13EC F3 AB                rep stos    dword ptr es:[edi]       7: int x, y;     8: x = y = 0;013B13EE C7 45 EC 00 00 00 00 mov         dword ptr [y],0  013B13F5 8B 45 EC             mov         eax,dword ptr [y]  013B13F8 89 45 F8             mov         dword ptr [x],eax       9: printf("%d %d %d\n", ++x, ++x, ++x);013B13FB 8B 45 F8             mov         eax,dword ptr [x]  013B13FE 83 C0 01             add         eax,1  013B1401 89 45 F8             mov         dword ptr [x],eax  013B1404 8B 4D F8             mov         ecx,dword ptr [x]  013B1407 83 C1 01             add         ecx,1  013B140A 89 4D F8             mov         dword ptr [x],ecx  013B140D 8B 55 F8             mov         edx,dword ptr [x]  013B1410 83 C2 01             add         edx,1  013B1413 89 55 F8             mov         dword ptr [x],edx  013B1416 8B F4                mov         esi,esp  013B1418 8B 45 F8             mov         eax,dword ptr [x]  013B141B 50                   push        eax  013B141C 8B 4D F8             mov         ecx,dword ptr [x]  013B141F 51                   push        ecx  013B1420 8B 55 F8             mov         edx,dword ptr [x]  013B1423 52                   push        edx  013B1424 68 B8 58 3B 01       push        13B58B8h  013B1429 FF 15 14 91 3B 01    call        dword ptr ds:[13B9114h]  013B142F 83 C4 10             add         esp,10h  013B1432 3B F4                cmp         esi,esp  013B1434 E8 07 FD FF FF       call        __RTC_CheckEsp (013B1140h)      10: printf("%d %d %d\n", y--, ++y, y);013B1439 8B 45 EC             mov         eax,dword ptr [y]      10: printf("%d %d %d\n", y--, ++y, y);013B143C 83 C0 01             add         eax,1  013B143F 89 45 EC             mov         dword ptr [y],eax  013B1442 8B 4D EC             mov         ecx,dword ptr [y]  013B1445 89 8D 24 FF FF FF    mov         dword ptr [ebp-0DCh],ecx  013B144B 8B 55 EC             mov         edx,dword ptr [y]  013B144E 83 EA 01             sub         edx,1  013B1451 89 55 EC             mov         dword ptr [y],edx  013B1454 8B F4                mov         esi,esp  013B1456 8B 45 EC             mov         eax,dword ptr [y]  013B1459 50                   push        eax  013B145A 8B 4D EC             mov         ecx,dword ptr [y]  013B145D 51                   push        ecx  013B145E 8B 95 24 FF FF FF    mov         edx,dword ptr [ebp-0DCh]  013B1464 52                   push        edx  013B1465 68 B8 58 3B 01       push        13B58B8h  013B146A FF 15 14 91 3B 01    call        dword ptr ds:[13B9114h]  013B1470 83 C4 10             add         esp,10h  013B1473 3B F4                cmp         esi,esp  013B1475 E8 C6 FC FF FF       call        __RTC_CheckEsp (013B1140h)      11: return 0;013B147A 33 C0                xor         eax,eax      12: }

 

---> Difference in computing ++ X and X ++: computing ++ takes out the value of X in the register, adds 1 to the register, and writes it back to X, X ++ retrieves X to the register, stores the value in an allocated address, adds 1 to the register, and writes the result back to X, however, in the output, ++ x outputs the value of X, X ++ outputs the values stored in the allocated address before x plus 1 (the address value will be put into the register before the value in the register is written into the stack)


Int x = 0;

Printf ("% d", X ++) --> 2 1 0 from right to left, from right to left into Stack

Take out x (0) --> put into the register --> assign an address 1 to store X (0) --> X auto-increment 1 --> write back X --> cause x = 1;

Retrieve x (1) --> put into Register --> assign an address 2 to store X (1) --> X auto-increment 1 --> write back X --> Cause X = 2;

Retrieve X (2) --> put into Register --> assign an address 3 Store X (2) --> X auto-increment 1 --> write back X --> Cause X = 3;

--> Extract content from address 1 address 2 address 3 in sequence and push the content to the stack

Outbound stack output element 2 1 0






++ I and I ++ output

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.