You have asked very similar questions in your previous posts where I explained how pre- and post-increments/decrements work:
Also, you should mark the correct answer with green and the wrong one with red, not the other way around (as it is right now).
I've looked into this more closely and yes, you are right, OnlineGDB says the correct answers are 31 and 36 (correspondingly). Why?! Well...
I've found a similar thread online: https://www.element14.com/community/thread/46302/l/multiple-pre-incrementpost-increment-in-expression-of-c-language
The whole point is that the code in the example has undefined behaviour. That is, it's not a valid C code and should have not been written in the first place. The bahaviour may vary from compiler to compiler.
In C++ you can override the pre- and post-increment operators, and you could actually "Debug" what's going on:
MyInt(int value) :
MyInt(const MyInt & other):
MyInt & operator++()
std::cout << "++MyInt --> " << _value << std::endl;
//operator++(); // prefix-increment this instance
std::cout << "MyInt++ --> " << tmp._value << std::endl;
return tmp; // return value before increment
operator int() const
MyInt i = MyInt(14);
printf("%d\n", ++i + i++);
int i2 = 14;
printf("%d\n", ++i2 + i2++);
The output is this:
++MyInt --> 15
MyInt++ --> 15
It's clearly visible that using your own class (MyInt) and the built in value type int has different results
So why 31... The answer is: no. No. That question is pointless, the code has undefined behaviour. Other undefined behaviours would be the memory address of an uninitialised pointer, or trying to modify a string literal, etc. The C standard doesn't specify what should happen. Various compilers do their own logic.
I hope you don't leave more confused than when you asked the questions. :)