23rd Oct 2017, 7:24 AM
Jaya Goswami
10 Answers
+ 2
Hmm. Think this like this: int y = ++x * ++x is equal to: x = x + 1 //6 x = x + 1 //7 int y = x * x //49 So the multiplication happens after increments happen. This is because prefix increments (++x) happens before the statement executes.
23rd Oct 2017, 8:02 AM
Mert Açıkportalı
Mert Açıkportalı - avatar
+ 3
x becomes 6 at the first part and then 7 at the second part. After that the multiplication happens so it is 7*7. I think this is why.
23rd Oct 2017, 7:32 AM
Mert Açıkportalı
Mert Açıkportalı - avatar
+ 3
Ah, also, the output will be 742 in Java, C# languages like your prediction. C++ has some undefined behaviour. For more information again check this site: https://stackoverflow.com/questions/6457130/pre-post-increment-operator-behavior-in-c-c-java-c-sharp
23rd Oct 2017, 8:33 AM
Mert Açıkportalı
Mert Açıkportalı - avatar
+ 2
but it should be.. 742 according to logic. which says ++x(value 6)*++x(value7) that's 42
23rd Oct 2017, 7:27 AM
Jaya Goswami
+ 2
exactly.. but why we get 7*7=49 as output
23rd Oct 2017, 7:31 AM
Jaya Goswami
+ 2
but the 1st value of ++x should be taken as 6 & next ++x should be taken as 7, what's the reason of both being taken as 7, but it's not in the other cases of x++ or --x.
23rd Oct 2017, 7:58 AM
Jaya Goswami
+ 2
This time x++ is postfix and it effects the right x value but doesn't increment itself before the statement executes. You can check this link to get deep information about these: https://stackoverflow.com/questions/7031326/what-is-the-difference-between-prefix-and-postfix-operators
23rd Oct 2017, 8:22 AM
Mert Açıkportalı
Mert Açıkportalı - avatar
+ 2
okay okay got u.. thanks!
23rd Oct 2017, 8:23 AM
Jaya Goswami
+ 1
x=5; y=x++*x; cout<<x<<y; gives 630 ie 6, 5*6 as o/p . why not it takes the 6 as both the values?
23rd Oct 2017, 8:08 AM
Jaya Goswami