# Thread: Output Problem

1. ## Output Problem

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();
}```

2. post increment (i++) changes the variable after the expression. the line before "j =..." i = 10, the line after assigning j, i has actually changed

3. 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.

4. 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("&#37;d",j);
getch();
}```

5. 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.
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.

6. Originally Posted by sunny_master_07
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();
}```
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.

7. if it helps to know, in Java the answer is 21, as OP expected!

8. Originally Posted by manav
if it helps to know, in Java the answer is 21, as OP expected!
...Yes, and that's Java. Not C. Different rules in effect.

9. 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```

10. Originally Posted by manav
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.

11. `'.,~bIgMeSs~.'`

such nerdy behavior is always annoying to me!

12. 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.
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.

13. ## hello it is defends on Compiler...

Originally Posted by sunny_master_07
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();
}```
Becasue:-
1.The bahavior undefined and completly defends on which compiler you are using??

Popular pages Recent additions