自增大总结:i=(i++)+(++i)另i=?

来源:互联网 发布:pyside 网络编程 编辑:程序博客网 时间:2024/05/21 07:01

源代码:

#include <stdio.h>

int main()
{
  int i = 1, j = 1, k;

  i = (i++) + (++i);
  printf("i=%d /n", i);

  k = (j++) + (++j);
  printf("j=%d, k=%d/n", j, k);
}

执行结果:

i=5
j=3, k=4


关键的汇编码片段:

 movl $0x1,0xfffffff0(%ebp)
 movl $0x1,0xfffffff4(%ebp)
 addl $0x1,0xfffffff0(%ebp)
 mov 0xfffffff0(%ebp),%eax
 add %eax,0xfffffff0(%ebp)
 addl $0x1,0xfffffff0(%ebp)
 mov 0xfffffff0(%ebp),%eax
 mov %eax,0x4(%esp)
 movl $0x8048500,(%esp)
 call 80482f8 <printf@plt>
 addl $0x1,0xfffffff4(%ebp)
 mov 0xfffffff4(%ebp),%eax
 add 0xfffffff4(%ebp),%eax
 mov %eax,0xfffffff8(%ebp)
 addl $0x1,0xfffffff4(%ebp)
 mov 0xfffffff8(%ebp),%eax
 mov %eax,0x8(%esp)
 mov 0xfffffff4(%ebp),%eax
 mov %eax,0x4(%esp)
 movl $0x8048507,(%esp)
 call 80482f8 <printf@plt>

发现,先执行++i,最后执行i++。

i = (i++) + (i++),i是不是等于2(0+0,接着i两次自加)
j = (i++) + (i++),j是不是等于0 (i没自加前把值赋给了j)