Why the output of this code is 9 not 10? Plz explain 🙏 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why the output of this code is 9 not 10? Plz explain 🙏

#include <iostream> using namespace std; int main() { int a=9; { int a=9; a++; } cout<<a++; return 0; }

18th Oct 2021, 2:35 PM
Jainil Raval
Jainil Raval - avatar
3 Answers
+ 3
scope. The second a belongs to a inner block code and a++ increments the a in its scope
18th Oct 2021, 2:38 PM
Arturop
Arturop - avatar
+ 1
Arturop Got it Thanks for explaining 🙏
18th Oct 2021, 3:19 PM
Jainil Raval
Jainil Raval - avatar
0
Because you use post-increment operator when you send output of <a>. Had you used pre-increment operator, you'll get 10 cout << ++a; // output 10
18th Oct 2021, 2:46 PM
Ipang