Programming in C
Programming Fundamentals
GATE 2008
Expert
What is printed by the following C program?
int f(int x, int *py, int **ppz) {
int y, z;
**ppz += 1; 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));
}- A18
- 19
- C21
- D22
Explanation
f is called with x=4 (copy of c, pass-by-value), py=b (points to c), ppz=a (points to b). **ppz += 1: c becomes 4+1=5. z = **ppz = 5. *py += 2: c becomes 5+2=7. y = *py = 7. x += 3: x becomes 4+3=7 (local copy only, doesn't affect c). Return x+y+z = 7+7+5 = 19.
Source: GATE CS 2008 (2008)
This question is part of a 15-day revision cycle — sign in and Project Forge will bring it back the day before you'd forget it.