2012/2/22 James Courtier-Dutton <james.dut...@gmail.com>: > The order that function parameters are evaluated is undefined. Therefore it > is wise to ensure that no matter what order they are evaluated, the result > should be the same. It is the ++ that breaks it in this case. Didn't you get > a compiler warning?
Yes you are right. gcc -Wall indeed get the warning. operation on 'a' may be undefined [-Wsequence-point] Thanks for you reminder. Let me know ,If we want the result we want ,we should do the precision logic design . > On Feb 21, 2012 3:19 PM, "嘉谟" <yxy....@gmail.com> wrote: >> >> I do a experiments to check how gcc pass the arguments. >> here is the code >> >> #include <stdio.h> >> int main(int argc , char *argv[]){ >> int a=3; >> int b=3; >> int c=3; >> printf("%d %d\n",++a+c,a+c); >> printf("%d %d\n",++b,b); >> return 0; >> } >> >> the anwer is >> >> 8 7 >> 4 4 >> >> the piece of assembly language: gcc 4.6.2 >> >> movl $3, 28(%esp) >> movl $3, 24(%esp) >> movl $3, 20(%esp) >> movl 20(%esp), %eax >> movl 28(%esp), %edx >> leal (%edx,%eax), %ecx >> addl $1, 28(%esp) >> movl 20(%esp), %eax >> movl 28(%esp), %edx >> addl %eax, %edx >> movl $.LC0, %eax >> movl %ecx, 8(%esp) >> movl %edx, 4(%esp) >> movl %eax, (%esp) >> call printf >> addl $1, 24(%esp) >> movl $.LC0, %eax >> movl 24(%esp), %edx >> movl %edx, 8(%esp) >> movl 24(%esp), %edx >> movl %edx, 4(%esp) >> movl %eax, (%esp) >> call printf >> >> In the first case , gcc first compute the a+c to %ecx ,and pass it >> stack , the compute ++a+c to %edx ,so the answer is 8 7 >> >> In the second case , why it didn't do the same thing like >> compute b=3 and pass it to stack ,then compute ++b and pass it to >> stack .to the result 4 3. However it first >> addl $1, 24(%esp) ==> b+1 I think it compute the expression >> b+1. the pass it to stack . the b which now is 4 and was passed to >> stack. >> >> I was wondering why gcc handle the same mode in two ways. >> >> Is there some errors I had made ? >> In my opinion ,for the reason of the concept of consistent ,it may be >> 8 8 >> 4 4 >> >> thank you advance