Output confusion | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Output confusion

as while outputting x it goes from right to left following stack but in y output it doesnt. is this because of operator precedence https://code.sololearn.com/cMX8qEhxBWQw/?ref=app

25th Jul 2017, 5:36 PM
anoneon
anoneon - avatar
10 Answers
+ 2
I also find it interesting that if I change justFill(a, x++, x, x); // 101111 to justFill(a, x++, (x), x); // 101010 The output changes on my compiler, but again not on sololearn
25th Jul 2017, 7:57 PM
Dennis
Dennis - avatar
+ 6
@Dennis: SoloLearn: GNUC (gcc) 4.8.1 https://code.sololearn.com/cu8tmoP4h80H/?ref=app @Dennis and @anoneon.... I got this warning: "Operation on 'x' may be undefined" while testing other gcc's (and I thought it was just mixing ++x and x++ in the same statement). It's still a "sequence point" problem, because there are no sequence points between function parameters....and this "<<" is a function in the cout template system. Using the "operator<<" expansion from the links (below + additional reading): cout << x << x++ << endl; // expands to, I believe: operator<< ( operator<< (operator<< (cout, x), x++), endl); If I'm reading the links below properly, the 'unspecified' interaction is between the sequenced function, the unsequenced parameters and the post-increment (operating at a sequence point): https://stackoverflow.com/questions/25466285/c-cout-side-effect-sequencing https://stackoverflow.com/questions/10782863/what-is-the-correct-answer-for-cout-c-c Behavior is (as @Dennis originally indicated) 'undefined' / 'unspecified'. * SoloLearn does not appear to warn like the others.
25th Jul 2017, 8:58 PM
Kirk Schafer
Kirk Schafer - avatar
+ 6
Of all the tasks I've done, working as an intrusion analyst honed: "yeah...but how/why?" into a bit of a trigger for me. :)
25th Jul 2017, 9:21 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
This is because of undefined behaviour. Sololearn outputs: 1110 1011 While my compiler outputs 1010 1011 It's kinda the same issue as with: arr[x] = x++; //Don't do this ^^ The compiler desides in which order it is evaluated.
25th Jul 2017, 6:21 PM
Dennis
Dennis - avatar
+ 2
@Dennis...would you mind checking your output on this? Here I see a traveling 10 but I suspect for you it will fill in a triangle main snippet, fills an array parameter (a, b or c): justFill(a, x++, x, x); justFill(b, y, y++, y); justFill(c, z, z, z++); SoloLearn output (the above, and): a : 101111 b : 111011 c : 111110 xyz: 111111 https://code.sololearn.com/cFt8yu3krN5R/?ref=app
25th Jul 2017, 6:59 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
@Dennis: That's interesting. This seems to imply an internal inconsistency in cout vs functions (and it feels like a compiler bug). Because of this I added a set of cout's with the same pattern as the function. SoloLearn produces the same output for both (which makes me wonder again if you'll now get a triangle).
25th Jul 2017, 7:46 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Damn, you sure put alot of research into this. Kudos to you. :)
25th Jul 2017, 9:06 PM
Dennis
Dennis - avatar
+ 1
Actually I get the same output as on sololearn, with the exception of the first line again 1110 -> 1010
25th Jul 2017, 7:02 PM
Dennis
Dennis - avatar
+ 1
Last 2 lines on my compiler are different Sololearn: 101111 111011 111110 GCC 7.1.0 (should you be interested) 101111 101011 101010
25th Jul 2017, 7:50 PM
Dennis
Dennis - avatar
+ 1
thanks all
26th Jul 2017, 3:51 AM
anoneon
anoneon - avatar