#include<stdio.h>
int main()
{
int a=2;
int j=++a + ++a + ++a + ++a ;
printf("%d",j);
}
//please tell me the execution of this fragment ... //i am using GCC ubuntu (12.04) compiler. -_-
#include<stdio.h>
int main()
{
int a=2;
int j=++a + ++a + ++a + ++a ;
printf("%d",j);
}
//please tell me the execution of this fragment ... //i am using GCC ubuntu (12.04) compiler. -_-
I generated the assembly code (gcc for windows on WIndows 7) [gcc -S test.c] and I get the following:
.file "increment.c"
.def ___main; .scl 2; .type 32; .endef
.section .rdata,"dr"
LC0:
.ascii "%d, %d\12\0"
.text
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
LFB6:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
andl $-16, %esp
subl $32, %esp
call ___main
movl $2, 28(%esp) ;28(%esp) = 2 (a=2)
incl 28(%esp) ;28(%esp) = 3 (a++)
incl 28(%esp) ;28(%esp) = 4 (a++)
movl 28(%esp), %eax ;%eax = 4 (eax = a = 4)
leal (%eax,%eax), %edx = %eax + %eax = 8 (edx = a + a)
incl 28(%esp) ;28(%esp) = 5 (a++)
movl 28(%esp), %eax ;%eax = 5 (eax = a = 5)
addl %eax, %edx ;%edx = %eax + %edx = 5 + 8 = 13 (edx = a + a + a)
incl 28(%esp) ;28(%esp) = 6 (a++)
movl 28(%esp), %eax ; %eax = 6 (eax = a = 6)
addl %edx, %eax ;%eax = %edx + %eax = 13 + 6 = 19 (eax = a + a + a + a)
movl %eax, 24(%esp)
movl 28(%esp), %eax
movl %eax, 8(%esp)
movl 24(%esp), %eax
movl %eax, 4(%esp)
movl $LC0, (%esp)
call _printf
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
LFE6:
.def _printf; .scl 2; .type 32; .endef
After computing the result (see comments) the printf function is called with 19 as result.
Anyway different compilers can give different results. The final result depends on the way the compiler arranges instructions.