What is the scope of a variable declared in a nested block inside a function?
Options
- A. Entire program
- B. The nested block in which it is declared
- C. Every function in the program
- D. More than one of the above
- E. None of the above
Quiz Practice:
B. The nested block in which it is declared
Answer: (B) The nested block in which it is declared
A variable declared inside a nested block has scope limited to that block.
For example:
void test() {
{ int value = 20; }
}
The variable value is available only inside its nested block. After the closing }, it is outside its scope.
Nested blocks are commonly used to create a smaller scope for temporary variables.