How!! and the second answer is right?!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How!! and the second answer is right?!!

#include <iostream> using namespace std; int main() { int x =5; cout<<x++<<x<<"\n"<<++x<<"\n"<<x<<endl; return 0; } -------_---------------------------------------- answer one: output this Code Editor 67 7 7 ================================ answer two : Output another Code Editor 56 7 7

15th Sep 2017, 11:17 AM
Ahmed Salah El-Kholy
Ahmed Salah El-Kholy - avatar
6 Answers
+ 16
it is called undefined behaviour. https://en.m.wikipedia.org/wiki/Undefined_behavior I believe these examples are only ever meant to be thought exercises. they are bad either way as they often confuse the student more than help. see http://en.cppreference.com/w/cpp/language/operator_incdec
15th Sep 2017, 11:29 AM
jay
jay - avatar
+ 12
seems odd to me. when the result can vary. i guess the compilers are all the same in the schools though
15th Sep 2017, 1:16 PM
jay
jay - avatar
+ 6
@jay These type of questions form the base of questions in computer science exams in my school. And then teacher verifies the answer by making us compile the codes, which can be differently approached in undefined behaviour...
15th Sep 2017, 12:54 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 6
@jay Unfortunately, they are not. Some still use Turbo. Others including me use CodeBlocks.
15th Sep 2017, 1:21 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 5
@Ahmed Salah El-Kholy If you execute from left to right, yes. If you execute from right to left, no. That my friend, is the dilemma we face here...
15th Sep 2017, 2:55 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
the second answer is right?!!
15th Sep 2017, 2:54 PM
Ahmed Salah El-Kholy
Ahmed Salah El-Kholy - avatar