Skip to main content

GATE CSE 2026Programming in C › Recursion & Program TracingVerified

GATE CSE 2026 PYQ: Consider the following program in C: #include <stdio.h> void func ( int i , int j ) { if ( i < j ) { int i =…

Consider the following program in C:

  1. #include <stdio.h>
  2. void func(int i, int j) {
  3. if(i < j) {
  4. int i = 0;
  5. while (i < 10) {
  6. j += 2;
  7. i++;
  8. }
  9. }
  10. printf("%d", i);
  11. }
  12. int main() {
  13. int i = 9, j = 10;
  14. func(i, j);
  15. return 0;
  16. }

The output of the program is ______\_\_\_\_\_\_. (answer in integer)

Note: Assume that the program compiles and runs successfully.

    Answer

    9

    Approach

    Trace the C program using C scope rules and call-by-value semantics. The crucial point is that the int i = 0; inside the if block creates a new block-local variable that shadows the parameter i. The final printf is outside that block, so it prints the parameter i of func, not the inner loop variable.

    The inner declaration int i = 0; is limited to the if block. The printf is outside that block, so it prints the unchanged parameter i = 9. The loop changes only the shadowing inner i and the local parameter j, neither of which affects the printed value.

    Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.