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

Correct Answer (Detailed Explanation is Below)

B. The nested block in which it is declared

Detailed Explanation

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.