What is printed by the following C program? $include <stdio.h> int f(int x,…
2008
What is printed by the following C program?
$include <stdio.h>
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));
getchar();
}
- A.
18
- B.
19
- C.
21
- D.
22
Attempted by 133 students.
Show answer & explanation
Correct answer: B
Answer: 19
Step-by-step evaluation:
Initial state in main: c = 4; b points to c; a points to b. Call f with x = 4 (passed by value), py = &c, ppz = &b.
**ppz += 1 increments the integer pointed to by *ppz, i.e., it increments c. After this, c = 5.
z = **ppz assigns z = 5.
*py += 2 adds 2 to c (via py), so c becomes 7; then y = *py gives y = 7.
x += 3 updates the local copy x from 4 to 7 (x in main is unaffected because x was passed by value).
Return value: x + y + z = 7 + 7 + 5 = 19, so printf prints 19.
Key point: modifications through py and ppz change the caller's variable c; x is a separate local copy.