Local/static variable scope in C++

You're confusing scope with lifetime. Static variables have a lifetime equal to the program's lifetime, but they still follow scoping rules based on where they are declared.


The scope of n is just between the brackets:

{int n;n=5;}

so outside of the block, you have no n variable.

Making it static just makes it's value retain even after you exit the block so that the next time you enter that block again, you can retrieve it's value from the last time you executed that block, but still it's scope is still within the brackets.