+ 2

What is the output

. . . int x=3; cout <<++x*++x*++x; .. . . . and plz also explain. ..... how it will

16th Nov 2016, 9:12 PM
Rajkumar Nagar
Rajkumar Nagar - avatar
6 Respostas
+ 3
This code has different results depending on which compiler you use because it has undefined behavior. Look at this page for explanations: http://en.cppreference.com/w/cpp/language/eval_order SoloLearn compiler doesn't output warning but gcc complains about an "unsequenced modification and access to 'x'" when trying to compile your example. Defined behavior is imposed in this code: https://code.sololearn.com/cX6QUicZ92Mb/#cpp
22nd Nov 2016, 2:43 PM
Jean-Hugues de Raigniac
Jean-Hugues de Raigniac - avatar
+ 2
Okay, so let's begin without the *. When you do ++x, you add 1 to x. You did it 3 times, so you add 3: one at the first ++x: x = 4, at the second: x = 5, at the third: x = 6. So, you'll have 4*5*6 = 120 ;)
16th Nov 2016, 9:20 PM
Volts
Volts - avatar
+ 2
cout << ++x*++x; prints 25, not 20. I can't explain why but it means both x hold value 5, not 4 and 5. Splitting the multiplication in 3 parts gives the correct answer.
16th Nov 2016, 10:23 PM
Jean-Hugues de Raigniac
Jean-Hugues de Raigniac - avatar
+ 1
turbo gives 120
16th Nov 2016, 9:22 PM
Rajkumar Nagar
Rajkumar Nagar - avatar
0
sorry @Volts gcc provides 150 but how
16th Nov 2016, 9:21 PM
Rajkumar Nagar
Rajkumar Nagar - avatar
0
This is a cascaded cout statement and usually evaluated from right to left and hence turbo gives 120 as the output because when evaluated from right to left we get 6*5*4 as x=3 which is 120!
21st Nov 2016, 9:27 AM
Chitransh Vishwakarma
Chitransh Vishwakarma - avatar