编译环境gcc4.4.1
如下代码
int a = 4;
a += (a++);
printf("%d\n",a);
a = 4;
a += (++a) ;
printf("%d\n",a);
a = 4;
(++a) += a;
printf("%d\n",a);
a = 4;
(a++) += a;//a++不能作为左值参加运算。
printf("%d\n",a);
a = 4;
(++a) += (a++);
printf("%d\n",a);
打印的值为9,10,10,11。汇编代码如下:
# a += (a++);
00401326 movl $0x4,0x1c(%esp)
0040132E mov 0x1c(%esp),%eax
00401332 add %eax,%eax
00401334 mov %eax,0x1c(%esp)
00401338 incl 0x1c(%esp)
0040133C mov 0x1c(%esp),%eax
00401340 mov %eax,0x4(%esp)
00401344 movl $0x46f024,(%esp)
0040134B call 0x41c750 <printf>
# a += (++a) ;
00401350 movl $0x4,0x1c(%esp)
00401358 incl 0x1c(%esp)
0040135C mov 0x1c(%esp),%eax
00401360 add %eax,%eax
00401362 mov %eax,0x1c(%esp)
00401366 mov 0x1c(%esp),%eax
0040136A mov %eax,0x4(%esp)
0040136E movl $0x46f024,(%esp)
00401375 call 0x41c750 <printf>
# (++a) += a;
0040137A movl $0x4,0x1c(%esp)
00401382 incl 0x1c(%esp)
00401386 mov 0x1c(%esp),%eax
0040138A add %eax,%eax
0040138C mov %eax,0x1c(%esp)
00401390 mov 0x1c(%esp),%eax
00401394 mov %eax,0x4(%esp)
00401398 movl $0x46f024,(%esp)
0040139F call 0x41c750 <printf>
#(++a) += (a++);
004013A4 movl $0x4,0x1c(%esp)
004013AC incl 0x1c(%esp)
004013B0 mov 0x1c(%esp),%eax
004013B4 add %eax,%eax
004013B6 mov %eax,0x1c(%esp)
004013BA incl 0x1c(%esp)
004013BE mov 0x1c(%esp),%eax
004013C2 mov %eax,0x4(%esp)
004013C6 movl $0x46f024,(%esp)
004013CD call 0x41c750 <printf>
可以看出,在运算过程中,++a优先级> += > a++
1)先运算a+a,然后再自增a
2),3)先运算++a,然后再a+a
4)先运算++a,然后a+a,然后a++