Question:

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
collegedunia
Verified By Collegedunia

Correct Answer: 9

Solution and Explanation

Step 1: Set up the initial values.
The program declares an integer pointer ptr and three integers a, b, c, then assigns
\[ a=5,\quad b=11,\quad c=20 \]

Step 2: Trace the pointer assignments.
The line
\[ \texttt{ptr=\&a;} \]
makes ptr point to a.
The next statement
\[ \texttt{*ptr=c;} \]
stores the current value of c (which is 20) into whatever ptr points to, that is, into a. So now
\[ a=20 \]
while b and c stay unchanged at 11 and 20.
Then
\[ \texttt{ptr=\&c;} \]
makes ptr point to c instead.

Step 3: Evaluate the next line.
\[ \texttt{a=*(\&b);} \]
Here \(\&b\) is the address of b, and dereferencing it with \(*\) just gives back the value stored in b, which is 11. So
\[ a=11 \]
At this point, a=11, b=11, and c is still 20 (unchanged so far).

Step 4: Evaluate the final assignment to c.
\[ \texttt{c=*ptr-a;} \]
Since ptr currently points to c, \(*ptr\) reads the value stored in c before this statement runs, which is 20. Then we subtract the current value of a, which is 11:
\[ c = 20 - 11 = 9 \]

Step 5: Find what gets printed.
The printf statement prints the value of c, which is now 9.

Final Answer:
\[ \boxed{9} \]
Was this answer helpful?
0
0