+ 2
What is the output
. . . int x=3; cout <<++x*++x*++x; .. . . . and plz also explain. ..... how it will
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
+ 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 ;)
+ 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.
+ 1
turbo gives 120
0
sorry @Volts gcc provides 150
but how
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!