Scope Tester

Scope Tester

Easy C Variables 45 views
Explanation Complexity

Problem Statement

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.

Input Format

No user input.
Nested blocks are created inside main().

Output Format

Value of x printed at:

• outer block

• middle block

• inner block

Example

Values assigned:

• outer x = 10

• middle x = 20

• inner x = 30
10
20
30
20
10

Constraints

• Same variable name used

• C block scope rules apply

Concept Explanation

In C, a variable declared in an inner block hides the variable with the same name from the outer block.

Step-by-Step Explanation

1.Create an outer block and declare int x = 10.

2.Print x → prints 10.

3.Create a middle block inside outer block.

4.Declare another int x = 20.

5.Print x → prints 20 (middle block value).

6.Create an inner block inside middle block.

7.Declare another int x = 30.

8.Print x → prints 30 (inner block value).

9.Exit inner block and print x → prints 20.

10.Exit middle block and print x → prints 10.

Concept Explanation

In C, a variable declared in an inner block hides the variable with the same name from the outer block.

Step-by-Step Explanation

1.Create an outer block and declare int x = 10.

2.Print x → prints 10.

3.Create a middle block inside outer block.

4.Declare another int x = 20.

5.Print x → prints 20 (middle block value).

6.Create an inner block inside middle block.

7.Declare another int x = 30.

8.Print x → prints 30 (inner block value).

9.Exit inner block and print x → prints 20.

10.Exit middle block and print x → prints 10.

Input / Output Format

Input Format
No user input.
Nested blocks are created inside main().
Output Format
Value of x printed at:

• outer block

• middle block

• inner block
Constraints
• Same variable name used

• C block scope rules apply

Examples

Input:
Values assigned: • outer x = 10 • middle x = 20 • inner x = 30
Output:
10 20 30 20 10

Example Solution (Public)

C
#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;
}

Official Solution Code

#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;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.