Scope Tester
C
Easy
2 views
Problem Description
Create nested blocks (outer, middle, inner). Use the same variable name 'x' in all three. Print the value at each level and understand which value is coming from which scope.
Official Solution
#include <stdio.h>
int main() {
int x = 10; // Outer block variable
printf("Outer block x = %dn", x);
{
int x = 20; // Middle block variable
printf("Middle block x = %dn", x);
{
int x = 30; // Inner block variable
printf("Inner block x = %dn", x);
}
// Back to middle block
printf("Middle block x (after inner) = %dn", x);
}
// Back to outer block
printf("Outer block x (after middle) = %dn", x);
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!