+ 5

What will be the output of this code and how? please explain this code.

main () { int x=3, y=4,z; z=x++ + ++y - x-- + --y; printf("%d ",z); } /* what will be the value of x, y and z; */

28th Jan 2018, 5:05 PM
vishal ranjan
vishal ranjan - avatar
5 odpowiedzi
+ 19
//"no output" will be the output of this code after removing some errors x will be 3 , y will be 4 & z will be 8 // z= 3 + 5 - 4 + 4
28th Jan 2018, 5:10 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 6
First of all, you should use better formatting for your question. The title should describe in short, what the question is about; there should be a language specified in the tags; the code needs to be in the question body and better formatted. Second, if the code is: int x = 3, y = 4, z; z = x++ + ++y - x-- + --y; then the code produces undefined behaviour, because you are modifying x and y more than once in one expression (sequence point). See the following question, and the links in it's answers for more info: https://www.sololearn.com/Discuss/989706/?ref=app
28th Jan 2018, 5:18 PM
deFault
+ 3
As a support for Marvin's answer: http://en.cppreference.com/w/cpp/language/operator_incdec Since this code causes Undefined Behaviour (UB) in C++, different compilers can have different ways to handle this code. These compilers don't treat the code the same way as sololearn compiler: http://coliru.stacked-crooked.com/ http://rextester.com/l/cpp_online_compiler_gcc This is a nice question, but try to apply good coding habits by avoiding theoretically confusing statements such as x=x++. Maybe you can rewrite the code in this way: // one possible meaning of z = x++ + ++y - x-- + --y; int x=3, y=4, z; x++; z = x; x--; z -= x; --y; z += y; ++y; z += y; std::cout<<"x = "<<x<<"\ny = "<<y<<"\nz = "<<z<<std::endl;
29th Jan 2018, 12:35 PM
Hanz
Hanz - avatar
+ 3
@Low Kai Han, Did you manage to find a compiler that produces a result different from (3, 4, 8)? Because I tried to find an example for this question and failed. I even compared the dissassembly, produced by gcc and clang (linked below), and the order of operations was exactly the same there. https://code.sololearn.com/c5yf9l596Yx4/?ref=app Anyway, even if all the compilers produced the same result, the fact that you shot yourself in the leg and missed, doesn't make doing it again a good idea. P.S. For people that are interested in this question but don't want to drill down the links in the question I linked above, here are most relevant direct links: http://en.cppreference.com/w/cpp/language/eval_order https://en.wikipedia.org/wiki/Sequence_point P.P.S. @Gaurav Agrawal, In your claim that z will be calculated as z= 3 + 5 - 4 + 4 you assume that C++ evaluates arithmetic expressions from left-to-right. This assumption is very common but it's not true. The expression may as well be evaluated in right-to-left or any other order. The operands may even be evaluated in parallel, wich will lead to race conditions in side effects. For example, if the expression would be executed right-to left, it will evaluate z as: z = 2 + 4 - 3 + 3.
29th Jan 2018, 1:48 PM
deFault
+ 2
https://code.sololearn.com/cAcEd9MTLkHF/?ref=app X=3 y=4 z=8 in java working normally
28th Jan 2018, 5:33 PM
SO Gamer
SO Gamer - avatar