- 3
What is output of int a=9{int a=9;a++}cout<<a++
13 Answers
- 6
I want explanation
+ 3
Click on your profile ,there you will see code bits ,click on + sign and then choose C++ language to check the output
+ 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.
+ 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
0
Avinesh actually it's a challenge question of sololearn
0
Avinesh there they didn't mention Preprossecor
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.
0
Avinesh https://ibb.co/jDDTT4Qhave a look n plz tell me how to solve
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 !
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.
- 1
Abhay i know that thing bro
I want explanation. I m not getting it