+ 4

Can any one explain why the output is different from one another programming language?

https://code.sololearn.com/cLekp2JxCN12/?ref=app https://code.sololearn.com/cf8fCSkqXJLl/?ref=app

10th Jun 2019, 4:03 PM
Programmer Raja
Programmer Raja - avatar
20 Respostas
+ 9
The behavior is undefined in C. The result isn't just different from Java, but it might be different for every compiler you compile the code with https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior
10th Jun 2019, 4:04 PM
Anna
Anna - avatar
+ 7
You can't say that the output is 2 in C because it is undefined behavior (see the link I posted). With another compiler you might get the same output as in Java, or something totally different
10th Jun 2019, 4:17 PM
Anna
Anna - avatar
+ 7
Don't Forget Key Last attempt, but the output in C and C++ is actually undefined
11th Jun 2019, 4:52 AM
Anna
Anna - avatar
+ 5
Anna have answered your question but you can still check on sequence point, that is what causes it actually which will arrive at undefine behavior
10th Jun 2019, 4:25 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 4
Exactly 👌
10th Jun 2019, 4:24 PM
Anna
Anna - avatar
+ 4
The result are same: 2. Why? Just don't forget prefix and sufix operand and differences between + and ++, - and -- respectively.
10th Jun 2019, 5:35 PM
Aleksandar Nedeljkovic
Aleksandar Nedeljkovic - avatar
+ 4
In case that interpreter/compiler make difference and gave us differently results, there's must be a problem in interpreter/compiler. Mathematic says completely solution and results.
10th Jun 2019, 5:37 PM
Aleksandar Nedeljkovic
Aleksandar Nedeljkovic - avatar
+ 4
Of course, if 2 + 2 equal to 4, 1 AND 0 equal 0, 1 AND 1 equal to 1, 0 OR 1 equal to 1... That what I would to say.
10th Jun 2019, 5:40 PM
Aleksandar Nedeljkovic
Aleksandar Nedeljkovic - avatar
+ 4
If you fist add value and, after that, make some action with variable, naturally that variable doesen't contain old value.
10th Jun 2019, 5:42 PM
Aleksandar Nedeljkovic
Aleksandar Nedeljkovic - avatar
+ 3
Yes Don't Forget Key . You're right. Pardon me for my quick answer without checking result.
11th Jun 2019, 6:53 AM
Aleksandar Nedeljkovic
Aleksandar Nedeljkovic - avatar
+ 3
Don't Forget Key I try to resolve question by looking at code, not test it.
11th Jun 2019, 6:54 AM
Aleksandar Nedeljkovic
Aleksandar Nedeljkovic - avatar
+ 2
No, I'm not confused with logical ops Don't Forget Key. I just follow logic đŸ€Ł P.S. AND, OR, NOT, NAND, NOR, XAND, XOR (eXclusive OR)...😎 I've got A in Mathematics Logic 😁
12th Jun 2019, 12:25 PM
Aleksandar Nedeljkovic
Aleksandar Nedeljkovic - avatar
+ 2
Why the result are 1 for me? I used Xperia XZ3 Don't Forget Key
12th Jun 2019, 12:27 PM
Aleksandar Nedeljkovic
Aleksandar Nedeljkovic - avatar
+ 2
Well, you add a comment after line now Don't Forget Key. It's okay. After all, variable "i" was inrement by one. In other words, variable "i" increments after other operations. If you work with ++i clause, than i was pre-increment. Good job 😎
12th Jun 2019, 4:12 PM
Aleksandar Nedeljkovic
Aleksandar Nedeljkovic - avatar