Question:medium

Consider the following ANSI-C program.

#include <stdio.h>

int main(){
    int *ptr, a, b, c;
    a=5; b=11; c=20;
    ptr=&a; *ptr=c; ptr=&c;
    a=*(&b); c=*ptr-a;
    printf("%d",c);
    return(0);
}
The output of this program is ______.

Note: Assume that the program compiles and runs successfully.

Show Hint

Track what ptr points to after each reassignment, and remember that in the statement "c=*ptr-a", the old value of c (before this line runs) is what gets read through *ptr.
Updated On: Jul 22, 2026
Show Solution

Correct Answer: 9

Solution and Explanation

Step 1: Make a small table and fill in the starting values.
$a=5,\ b=11,\ c=20,\ ptr=\text{undefined}$

Step 2: Update the table line by line.
After $ptr=\&a$: ptr points at a. Values unchanged: $a=5,b=11,c=20$.
After $*ptr=c$: the memory that ptr points to (which is a) gets the value of c, so $a=20$. Table now: $a=20,b=11,c=20$.
After $ptr=\&c$: ptr now points at c. Values unchanged: $a=20,b=11,c=20$.

Step 3: Continue with the next statement.
After $a=*(\&b)$: dereferencing the address of b just returns b's own value, $11$, so $a=11$. Table now: $a=11,b=11,c=20$.

Step 4: Apply the last statement carefully.
$c=*ptr-a$. Since ptr points at c, $*ptr$ reads c's value at that moment, which is still $20$ (the assignment has not happened yet). We subtract the current a, which is $11$. So
$c = 20-11=9$

Step 5: Read off the printed value.
printf prints c, and the final table shows $a=11,\ b=11,\ c=9$.
\[ \boxed{9} \]
Was this answer helpful?
0


Questions Asked in GATE CS exam