For how different compilers handle operator ++ and operator ++ =, the compiler operator

Source: Internet
Author: User

For how different compilers handle operator ++ and operator ++ =, the compiler operator

First, go to the Code:

#include <stdio.h>#include <iostream>using namespace std;int main(){int a = 5;printf("a++ = %d\n", a++);a = 5;printf("++a = %d\n", ++a);a = 5;printf("a += a++  =%d\n", a += a++);a = 5;printf("a += (a++)  =%d\n", a = a + (a++));a = 5;printf("a += ++a  =%d\n", a += ++a);a = 5;printf("a += (++a)  =%d\n", a += (++a));a = 5;printf("++a += a++ =%d\n", ++a += a++);a = 5;printf("(++a) += (a++) =%d\n", (++a) += (a++));return 1;}
I don't know what the readers think. What do you think is their output value?


The running result of VS2013 and the running result compiled by g ++ 4.8.2 are directly provided:


For different results, let's take a look at the assembly code (only the [printf ("a + = a ++ = % d \ n ", a + = a ++);] Partial Assembly is used as an example. You can take a look at it by yourself ):

/* vs2013 */    13: printf("a += a++  =%d\n", a += a++);011ECAC7  mov         eax,dword ptr [a]  011ECACA  add         eax,dword ptr [a]  011ECACD  mov         dword ptr [a],eax  011ECAD0  mov         ecx,dword ptr [a]  011ECAD3  mov         dword ptr [ebp-0D0h],ecx  011ECAD9  mov         edx,dword ptr [a]  011ECADC  add         edx,1  011ECADF  mov         dword ptr [a],edx  011ECAE2  mov         esi,esp  011ECAE4  mov         eax,dword ptr [ebp-0D0h]  011ECAEA  push        eax  011ECAEB  push        11F832Ch  011ECAF0  call        dword ptr ds:[11FC1E4h]  011ECAF6  add         esp,8  011ECAF9  cmp         esi,esp  011ECAFB  call        __RTC_CheckEsp (011E1631h)  


/* g++ 4.8.2 */13printf("a += a++  =%d\n", a += a++);   0x08048602 <+85>:mov    0x1c(%esp),%eax   0x08048606 <+89>:lea    0x1(%eax),%edx   0x08048609 <+92>:mov    %edx,0x1c(%esp)   0x0804860d <+96>:add    %eax,0x1c(%esp)   0x08048611 <+100>:mov    0x1c(%esp),%eax   0x08048615 <+104>:mov    %eax,0x4(%esp)   0x08048619 <+108>:movl   $0x8048814,(%esp)   0x08048620 <+115>:call   0x80484a0 <printf@plt>

I have read a few books, but the compilation is more self-developed. I hope you can help me explain the situation ~~~, Thank you!

[Note] the above Code running platform is a 32-bit operating system. vs2013 runs on Windows 8.1 Enterprise Edition, and g ++ runs on ubuntu14.04 (3.20.- 41-generic)

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.