Why doesn't the value decrease after decreasing it in cout ? Wondering | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why doesn't the value decrease after decreasing it in cout ? Wondering

Ex: #include <iostream> using namespace std; int main () { int a = 5 cout << a--; return 0; } The output would still be 5, why is this ?

3rd Dec 2016, 5:43 AM
Ash Ketchum
Ash Ketchum - avatar
5 Answers
+ 1
I think I figured it out, is it because I put it in console before decreasing it ? So now, if I used a again in another statement, it would then be 4 ?
3rd Dec 2016, 5:46 AM
Ash Ketchum
Ash Ketchum - avatar
+ 1
wouldn't you use --a in that case?
3rd Dec 2016, 10:34 PM
Vincent Kordiak
Vincent Kordiak - avatar
0
here post decrement operator is used so only in the next statement or in its next use of variable 'x' it will be decremented. use of pre decrement operator would decrement it first only then other tasks would be performed.
3rd Dec 2016, 6:24 AM
kv ajay
kv ajay - avatar
0
the value decreases but you never output it's value after the post decrement. if you output the value one more time you will see the value is 4
3rd Dec 2016, 7:02 AM
Elio
Elio - avatar
0
Thank you both.
3rd Dec 2016, 7:18 AM
Ash Ketchum
Ash Ketchum - avatar