The key issue is whether the side effects of the expression (n++)
takes place before or after the value of the expression is assigned to
n.
C and C++ specification requires all side effects ( including
assignment) of the current statement to take place before the next
statement, but does not specify in which order they take place, and
thus it's up to specific compiler implementation. Whereas I believe
both Java and C# specifications have specified that all side effects
must take effect before the assignment hence the discrepancy.
In Visual Studio 2008, disassembly shows:
int n;
n = 10;
004113BE mov dword ptr [n],0Ah
n = n++;
004113C5 mov eax,dword ptr [n]
004113C8 mov dword ptr [n],eax
004113CB mov ecx,dword ptr [n]
004113CE add ecx,1
004113D1 mov dword ptr [n],ecx
which is essentially:
n = 10;
n = n;
temp = n + 1;
n = temp;
where as GCC 4.3.2 disassembly ( w/o optimization ) shows:
main:
.LFB2:
pushq %rbp
.LCFI0:
movq %rsp, %rbp
.LCFI1:
movl $10, -4(%rbp)
addl $1, -4(%rbp)
leave
ret
which is essentially:
n = 10;
n = n + 1;
Both implementations happen to produce the same behaviour despite the
assemblies produced are quite different.
On Jun 5, 2:20 am, Shoubhik <[email protected]> wrote:
> Hello,
>
> in C,
>
> the following code:
>
> int n=10;
> n=n++;
>
> assigns 'n' a value of 11 .
>
> in Java,
> the same code doesnt increment 'n'... it remains 10 !!!
>
> plzzz explain...
>
> Regards,
> Shoubhik Bose.
--
You received this message because you are subscribed to the Google Groups
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-code?hl=en.