Consider the C program shown below. #include <stdio.h> #define print(x)…
2003
Consider the C program shown below.
#include <stdio.h>
#define print(x) printf("%d ", x)
int x;
void Q(int z)
{
z += x;
print(z);
}
void P(int *y)
{
int x = *y + 2;
Q(x);
*y = x - 1;
print(x);
}
main(void)
{
x = 5;
P(&x);
print(x);
}
The output of this program is
- A.
12 7 6
- B.
22 12 11
- C.
14 6 6
- D.
7 6 6
Attempted by 162 students.
Show answer & explanation
Correct answer: A
Final output: 12 7 6
Step 1: In main, set the global x to 5 and call P with the address of the global x.
Step 2: Inside P, compute the local x as *y + 2 = 5 + 2 = 7, then call Q with z = 7.
Step 3: In Q, add the global x (5) to z: z = 7 + 5 = 12. Print z, producing the first output value 12.
Step 4: Return to P. Set *y = local x - 1 = 7 - 1 = 6, so the global x becomes 6.
Step 5: Print the local x (7) inside P, producing the second output value 7.
Step 6: Return to main and print the global x (6), producing the third output value 6.
Therefore the program prints: 12 7 6