Hi.
When I execute this program I get output of j as 20. Why it is not 21? Can anyone explain. Thanks.
Code:int main(){ int i =10; int j; j = (i++ + i++); printf("%d",j); getch(); }
Hi.
When I execute this program I get output of j as 20. Why it is not 21? Can anyone explain. Thanks.
Code:int main(){ int i =10; int j; j = (i++ + i++); printf("%d",j); getch(); }
post increment (i++) changes the variable after the expression. the line before "j =..." i = 10, the line after assigning j, i has actually changed
Actually, the behaviour is undefined. 21 is also an acceptable output, but it so happens that you did not get it. Read this C FAQ on expressions. In particular, check out question 3.2.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
...Isn't that operation undefined to begin with?
http://zamanbakshifirst.blogspot.com...ue-of-i-i.html
http://www.research.att.com/~bs/bs_f...aluation-order
Ok....
But if i execute this program. It gives me 22 as output. Here it is possible because the value of i is changed within the expression. But in above case you are saying that it is 20 because i++ changes the variable after the expression. Please comment.
Code:int main(){ int i =10; int j; // j = (i++ + i++); j = (i++ + ++i); printf("%d",j); getch(); }
Both Lithorien and I have pointed out that there is undefined behaviour at work here. Any int value is a possible output, as is crashing, erasing your hard disk drive, launching a nuclear missle, etc.But if i execute this program. It gives me 22 as output. Here it is possible because the value of i is changed within the expression. But in above case you are saying that it is 20 because i++ changes the variable after the expression. Please comment.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It's entirely possible that your particular implementation has decided that "i++ + ++i" is treated like PHP, and is doing a right-to-left evaluation of that statement. Meaning, it increments I (because of the right hand operation), adds them, and then increments i again (because of the left hand operation). I can't promise you that, but that's my best-guess.
HOWEVER.
Relying on ANY undefined operation to do work is a horrible idea. Your program can act differently between compilers, operating systems, or hell, even a different version of the same compiler! It makes your code instantly non-portable, and is just an all around bad, bad idea. Avoid it.
if it helps to know, in Java the answer is 21, as OP expected!
there are same rules, in both, Java and C (and C++ also)
Code:i++; // return the value of i, then increment ++i; // increment the value of i, then return
And yet... http://forum.java.sun.com/thread.jsp...sageID=9589354
Similar problem, different results. Just because one implementation of one language does it one way doesn't mean it'll be done that way across all implementations of all languages where the semantics are the same.
`'.,~bIgMeSs~.'`
such nerdy behavior is always annoying to me!
I believe that Java specifies the precise evaluation order in this case. Of course, because it makes code more difficult to read, it should still be avoided in Java.Similar problem, different results. Just because one implementation of one language does it one way doesn't mean it'll be done that way across all implementations of all languages where the semantics are the same.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)