What is output of int a=9{int a=9;a++}cout<<a++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 3

What is output of int a=9{int a=9;a++}cout<<a++

2nd Jun 2020, 11:13 AM
Amisha Jha
Amisha Jha - avatar
11 Answers
- 6
I want explanation
2nd Jun 2020, 11:27 AM
Amisha Jha
Amisha Jha - avatar
+ 3
Click on your profile ,there you will see code bits ,click on + sign and then choose C++ language to check the output
2nd Jun 2020, 11:20 AM
Abhay
Abhay - avatar
+ 2
Amisha Jha as Martin Taylor already pointed out, every variable has a scope which is limited to the block it is declared in. A block is generally a set of curly braces { }. The variable 'a' defined outside the block has a global scope and can be accessed any where throughout the program but the one inside { } has scope limited to that block and cannot be accessed outside. Since it is post increment, the value is first printed and then it is incremented.
2nd Jun 2020, 12:24 PM
Avinesh
Avinesh - avatar
+ 1
Amisha Jha Is that the complete code you have there? No preprocessor directives, no main method, no semicolons? Please frame a proper question and include the details in the description and not in the title. https://www.sololearn.com/discuss/333866/?ref=app
2nd Jun 2020, 11:37 AM
Avinesh
Avinesh - avatar
0
Avinesh actually it's a challenge question of sololearn
2nd Jun 2020, 11:40 AM
Amisha Jha
Amisha Jha - avatar
0
Avinesh there they didn't mention Preprossecor
2nd Jun 2020, 11:40 AM
Amisha Jha
Amisha Jha - avatar
0
In that case you can take a screenshot of the challenge and post it in your feed then share that link here in your question. This will help us to quickly answer your question rather guessing the answer.
2nd Jun 2020, 11:46 AM
Avinesh
Avinesh - avatar
0
Avinesh https://ibb.co/jDDTT4Qhave a look n plz tell me how to solve
2nd Jun 2020, 12:15 PM
Amisha Jha
Amisha Jha - avatar
0
int a=9; { int a=9; a++; } cout<<a++; If you will write it in this way . Then it will simply give 9 as answer because a++ is a post increment and the code inside Curley bracket dosent have any meaning. That's all. and what about variable scopes !
2nd Jun 2020, 12:16 PM
Ayush Kumar
Ayush Kumar - avatar
0
int a=9; { int a=9; a++; } cout<<a++; the output is 9 only. because 'a' declared in the block only have the block scope. The life time of the 'a' which declared in the block is the block itself. So that will not affect the outside of the block.
2nd Jun 2020, 1:40 PM
Purushoth
Purushoth - avatar
- 1
Abhay i know that thing bro I want explanation. I m not getting it
2nd Jun 2020, 11:32 AM
Amisha Jha
Amisha Jha - avatar