Guys explain to me about increment and decrement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Guys explain to me about increment and decrement

. . Int x=7; Int y=4; Cout<<x++ * y--; // how is the out put 28??????

1st Aug 2022, 2:28 PM
Muluken
Muluken - avatar
4 Answers
+ 3
there is an order for these actions if you do: int x = 5; cout << x++ + 5; you will see 10, because 5 + 5 = 10, but after this line, x is now 6. if you want the increment to happen first, you can use ++x cout << ++x + 5; now you will see a different result 11.
1st Aug 2022, 2:43 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 2
cout<<x++ * y--; x++ return 7 but the value of x is now 8 y-- return 4 but the value of y is now 3 So 7*4 output become 28
1st Aug 2022, 3:59 PM
Mihir Lalwani
Mihir Lalwani - avatar
+ 2
10 q guys its all clear now Apollo-Roboto Mihir Lalwani
1st Aug 2022, 4:03 PM
Muluken
Muluken - avatar
+ 2
7*4 = 28, so the output is 28. postcrement is implemented like this: int post_add(int* x){ auto tmp = *x; *x = (*x)+1; return tmp; } you see the incremented value gets used at no time here. But be careful how you use post and precrements especially as arguments to functions. There is many strange undefined behaviour to when what get's evaluated. (cppcast did a good episode on that)
1st Aug 2022, 7:06 PM
Erarnitox
Erarnitox - avatar