0
Explain this code ......
int main() { int x = 0 cout << (--x) + (--x) ; } // output -4 how ??
6 Answers
+ 1
Well... no you're wrong đ
But you have understood.
However, when you write
y=(--x)+(--x)+(--x);
C++ thinks this :
y=(--x)+(--x);
y=y+(--x);
Soooo :
y=(--x)+(--x) ;// -2+-2=-4
y=y+(--x);// -4+-3=-7
--x //-1
--x //-2
x+x // x=-2
--x //-3
-2+x // x=-3
inctementation is fantastic but it becomes quick very very complicated as you can see.
If you don't understand ask me, don't hesitate!
+ 1
that's a good question
--x changes x value before execute the statement.
So in this code we have :
(--x) // x= -1
(--x) // x= -2
AND HERE IS THE THING :
we don't have -1 + -2
we still have x + x // and x= -2
so -2 + -2 = -4
Ask if you don't understand
+ 1
ya.. i was wrong :) but now
i have understood.
if we have four (--x) .
// cout << (--x) + (--x) + (--x) + (--x) ;
then
a = (--x) + (--x) //1st and 2nd (--x)
a = a + (--x) //3rd (--x)
a = a + (--x) //4th (--x)
so answer is
-11 :)
+ 1
Exactly !
Now you can enjoy incrementation in C++ !
+ 1
Thank you :)
for explanation
0
@jojo
if we have one more (--x) in COUT
// cout << (--x) + (--x) + (--x) ;
then x value is
(--x) // x = -1
(--x) // x = -2
(--x) // x = -3
and x+x+x // x = -3
so answer is -9
am i right ??