Skip to content
Official PYQ · 2008

GATE CS 2008 Q60 — Programming in C

Explain with AI

What is printed by the following C program?

int f(int x, int *py, int **ppz)			
{						                    	
    int y, z;					                		
    **ppz += 1; z = **ppz;		 // corrected z = *ppz; to z = **ppz;                
    *py += 2; y = *py;					                	
    x += 3;					                    	
    return x+y+z;
}

void main()
{
    int c, *b, **a;
    c = 4; b = &c; a = &b;
    printf("%d", f(c, b, a));
    	
}
Choose one option
View worked solution

Answer: B

Let's trace the execution of the program step-by-step:

1. Inside main():

  • c = 4
  • b points to c (i.e., *b is c)
  • a points to b (i.e., **a is c)

When calling f(c, b, a):

  • x is passed by value, so x = 4 initially. Changes to x do not affect c.
  • py points to c (via b). Modifying *py modifies c.
  • ppz points to c (via a and b). Modifying **ppz modifies c.

2. Inside f():

  • **ppz += 1;: Since **ppz refers to c, c becomes 4+1=54 + 1 = 5.
  • z = **ppz;: z becomes 55.
  • *py += 2;: Since *py refers to c, c becomes 5+2=75 + 2 = 7.
  • y = *py;: y becomes 77.
  • x += 3;: The local variable x becomes 4+3=74 + 3 = 7.

3. Return value:

The function returns x + y + z, which is 7+7+5=197 + 7 + 5 = 19. Therefore, the program prints 19.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions