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 ____________. (answer in integer)
Note: Assume that the program compiles and runs successfully.

Show Hint

Track carefully which variable \(ptr\) is aliasing at each step - it points first to \(a\), then is redirected to \(c\). Remember \(*(&b)\) simplifies to just \(b\).
Updated On: Jul 7, 2026
Show Solution
collegedunia
Verified By Collegedunia

Correct Answer: 9

Solution and Explanation

We trace the program line by line, tracking the values of the variables \(a\), \(b\), \(c\) and the pointer \(ptr\).

Step 1: Initial assignments.

\[ a = 5, \quad b = 11, \quad c = 20 \]

Step 2: Execute ptr = &a;

Now \(ptr\) holds the address of \(a\), so \(ptr\) points to \(a\).

Step 3: Execute *ptr = c;

This stores the current value of \(c\) (which is 20) into the location pointed to by \(ptr\), i.e. into \(a\). So now:

\[ a = 20 \]

Step 4: Execute ptr = &c;

Now \(ptr\) is redirected to point to \(c\) instead of \(a\).

Step 5: Execute a = *(&b);

The expression \(*(\&b)\) is simply \(b\) (dereferencing the address of \(b\) gives back \(b\)). Since \(b = 11\):

\[ a = 11 \]

Step 6: Execute c = *ptr - a;

Since \(ptr\) points to \(c\), \(*ptr\) is the current value of \(c\), which is still 20 (unchanged since Step 1). So:

\[ c = 20 - a = 20 - 11 = 9 \]

Step 7: The printf("%d", c); statement prints the current value of \(c\).

Final boxed answer:

\[ \boxed{c = 9} \]

Was this answer helpful?
0
0

Top GATE CS Computer Science and IT Engineering Questions

View More Questions

Top GATE CS Programming in C Questions