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); } }
- A.
30,10
- B.
30,20
- C.
20,30
- D.
More than one of the above
- 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.