int a=1;
执行sa=a++ 或 sa=(a++);后sa的值是一样的,都是1;
两个程序编译后的汇编代码完全一样,如下:
.file "eee.c"
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC0:
.ascii "a=%d sa=%d\0"
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
andl $-16, %esp
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
movl %eax, -12(%ebp)
movl -12(%ebp), %eax
call __alloca
call ___main
movl $1, -4(%ebp)
movl -4(%ebp), %edx
leal -4(%ebp), %eax 如果是++a则编译后这句和下一句顺序会交换。
incl (%eax)
movl %edx, -8(%ebp)
movl -8(%ebp), %eax
movl %eax, 8(%esp)
movl -4(%ebp), %eax
movl %eax, 4(%esp)
movl $LC0, (%esp)
call _printf
movl $0, %eax
leave
ret
.def _printf; .scl 2; .type 32; .endef
执行sa=++a后的sa的值是2;
#include "stdio.h"
int main(){
int a[5]={0,1,2,3,4};
int i=0,b;
b=a[i++]; //不同点在这里
printf("%d %d\n",i,b);
return 0;
}
的执行结果是1,0
#include "stdio.h"
int main(){
int a[5]={0,1,2,3,4};
int i=0,b;
b=a[++i]; //不同点在这里
printf("%d %d\n",i,b);
return 0;
}
阅读(1220) | 评论(0) | 转发(0) |