"n++" is an expression; it has a value. What it does is to increment n but return the value it had before incrementing it. The C result is extremely weird and can only be explained by diving into the particulars of how the compiler works. The java result, on the other hand, is semantically correct.
In other words, in java, this happens: n holds value 10 [after int n = 10] n holds value 11 [after n++] n = 10 [after n = (result of n++)] semantically correct end result: n = 10. Which it is, at least in java. Note also that '=' has a very low precedence level in java (the very lowest, in fact - depending on how you count, number 14), whereas ++ has the unary level (level 1). Therefore, n = n++ is definitely parsed as n = (n++); and not (n = n)++ which, if post-incrementing expressions were legal (which at least in java isn't legal), semantically would have meant that at the end, n should have been 11 and not 10. The real WTF here is why you would write "n = n++" - that's effectively a bug; a style checker would be more than justified in flagging that as a downright error and refuse to allow your code to be sent to the compiler. There's no point in doing that other than confusing folks. As a practical matter then, what "n = n++" actually means isn't a relevant question. Just funny to us compiler geeks. On Jun 5, 9: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.
