3 ms·
I get that the answer is "undefined". But I decided to figure out why at least on my compiler I get "14". $ cat t.c ; gcc ./t.c #include<stdio.h>
by mds 14y ago
I get that the answer is "undefined". But I decided to figure out why at least on my compiler I get "14".
$ cat t.c ; gcc ./t.c
#include<stdio.h>
int main() {
printf("%d\n", a());
}
int a() {
int i = 5;
i = ++i + ++i;
return i;
}
$ ./a.out
14 # ??
$ gcc t.c -c -o t.bin
$ gdb t.bin
...
(gdb) disassemble a
Dump of assembler code for function a:
0x0000000000000030 <a+0>: push %rbp
0x0000000000000031 <a+1>: mov %rsp,%rbp
0x0000000000000034 <a+4>: movl $0x5,-0xc(%rbp)
0x000000000000003b <a+11>: mov -0xc(%rbp),%eax
0x000000000000003e <a+14>: add $0x1,%eax # add 1 and store it in i => 6
0x0000000000000041 <a+17>: mov %eax,-0xc(%rbp)
0x0000000000000044 <a+20>: mov -0xc(%rbp),%eax
0x0000000000000047 <a+23>: add $0x1,%eax # add 1 and store it in i => 7
0x000000000000004a <a+26>: mov %eax,-0xc(%rbp)
0x000000000000004d <a+29>: mov -0xc(%rbp),%eax
0x0000000000000050 <a+32>: mov -0xc(%rbp),%ecx # move current value of i (7) into %ecx and %eax (both registers are now 7)
0x0000000000000053 <a+35>: add %ecx,%eax # add them together => 14
0x0000000000000055 <a+37>: mov %eax,-0xc(%rbp)
...