What is the output of the following C program? #include int main() { double a[2]={20.0, 25.0}, *p, *q; p = a; q = p + 1; printf("%d,%d", (int)(q - p), (int)(*q - *p)); return 0; }
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2024 Set 2 · Q36
Time
#include <stdio.h>
int main() {
double a[2]={20.0, 25.0}, *p, *q;
p = a;
q = p + 1;
printf("%d,%d", (int)(q - p), (int)(*q - *p));
return 0;
}Select one answerSingle correct
Solution
Official answer: B) 1,5
Programming in CLet us trace the given C program step-by-step:
Array and Pointer Initialization:
a is an array of two double elements: a[0] = 20.0 and a[1] = 25.0.
p and q are pointers of type double*.
p = a; makes pointer p point to the first element a[0] (address &a[0]).
q = p + 1; performs pointer arithmetic, making pointer q point to the next double element a[1] (address &a[1]).
Evaluating (int)(q - p):
In C, pointer subtraction (q - p) between two pointers of the same type yields the number of elements of that base type separating the two pointers, not the byte distance.
Since q points to a[1] and p points to a[0], the pointer difference is 1.
Casting to int gives 1.
**Evaluating (int)(*q - *p):**
Dereferencing *q gives 25.0 and *p gives 20.0.
The arithmetic difference is .
Casting to int gives 5.
Output:
The printf format string is "%d,%d", which prints 1,5.
Hence, the correct option is (B).
Try the question first, then check your work.
Examiner trap
Recall that pointer subtraction (q - p) gives the number of array elements between two pointers of the same type, not the difference in memory addresses in bytes.
This Programming in C previous year question is Q36 of the GATE CSE 2024 Set 2 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.
Free diagnostic
Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 19 of them are 2-mark MCQs like this one, and GATE CSE 2024 Set 2 has 4.
How GATE tests Programming in CIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.