What will be the output of the following program fragment? int i = 10; void…

2023

What will be the output of the following program fragment?

int i = 10; void main() { int i = 20; { int i = 30; printf("%d, %d", i, ::i); } }

  1. A.

    30,10

  2. B.

    30,20

  3. C.

    20,30

  4. D.

    More than one of the above

  5. E.

    None of the above

Attempted by 203 students.

Show answer & explanation

Correct answer: A

Step 1: The global variable i is initialized to 10. Step 2: Inside main(), a local variable i is declared and initialized to 20. Step 3: Inside the inner block, another local variable i is declared and initialized to 30. Step 4: The printf statement prints i (innermost block) and ::i (global variable). Step 5: Therefore, the output is 30,10.

Explore the full course: Uppsc Polytechnic Lecturer 2025 Cs