GATE CS 2024 Set 2 | Question 36

MCQ · 2 marks · −0.66

What is the output of the following C program?

#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;}
  1. Option A: 4,8
  2. Option B: 1,5 (correct)
  3. Option C: 8,5
  4. Option D: 1,8

Answer

(B)

Explanation

p points to a[0], and q = p + 1 points to the next element, a[1].

  • Subtracting two pointers into the same array counts elements, not bytes, so q - p is 1, even though a double usually takes 8 bytes.
  • *q - *p subtracts the stored values: \(25.0 - 20.0 = 5.0\), which the cast turns into the int 5.

So the program prints 1,5. Option (C) reports the pointer gap in bytes instead of elements, and the 4 and 8 in (A) and (D) match neither expression.

Answer: B