IAE Academy

GATE 2026 CS2 Q.60: solution and answer

GATE 2026 · CS2Q.60 · 2 marks · NAT

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.

See the question in the paper

Answer and solution

ANSWER

The program prints 9.

Official answer: 9Source: Official answer key, GATE 2026 CS2 (IIT Guwahati)

  1. Initialize: a=5, b=11, c=20.

  2. ptr=&a; sets ptr to point to a.

  3. *ptr=c; writes c's current value (20) into a, so a=20; c is unchanged at 20.

  4. ptr=&c; retargets ptr to point to c (it no longer points to a).

  5. a=*(&b); — &b dereferenced by * is simply b, so a=11 (overwriting the earlier a=20).

  6. c=*ptr-a; — since ptr points to c, *ptr yields c's current value, 20; so c = 20 - 11 = 9.

  7. printf("%d",c); outputs 9.

KEY IDEA

Track pointer targets and values separately: an assignment through *ptr changes whatever object ptr currently points to, not necessarily the same variable it pointed to earlier or later.

WATCH OUT FOR

Assuming *ptr always refers to a throughout the program (since ptr was first set to &a), and missing that ptr=&c; re-targets it to c before the final c=*ptr-a; line — this leads students to wrongly read *ptr as a's value there instead of c's.

From the GATE 2026 CS2 paper. Every question of this paper · Download the full paper · All 11 Pointers & arrays questions, with notes

More Pointers & arrays questions