+ 7

Explain output in C code 😐

Check these two codes, they do the same thing. One is written in C and other one in Java. Output is different! I understand output in Java, n=42 then incremented by 1, so n=43 then we check is it equal or less than 43, and we had n++ so n=44. Right? C ... hmmm, well I guess n is replaced by 42 in if statement and we have 43 <= 42 . 😐 Need an explanation for these two outputs https://code.sololearn.com/cMSR6c6x2k5y/?ref=app https://code.sololearn.com/cjBN76ozE71G/?ref=app

24th Oct 2018, 12:27 PM
voja
voja - avatar
2 Answers
+ 1
i guess its the case of C/C++ undefined behavior when doing pre and post increment at one statement we dont know which operation is done first.
24th Oct 2018, 2:17 PM
Taste
Taste - avatar
+ 1
It starts from the left side. In the such function ++42<=42++ the answer of this operation is 44<=42. C have always have undefined way but this what it does
1st Jan 2019, 3:57 AM
Code Man
Code Man - avatar