Skip to main content

GATE CSE 2024Programming in C › Pointers, Arrays & StringsVerified

GATE CSE 2024 PYQ: ​​​​​What is the output of the following C \text{C} C program? #include <stdio.h> int main () { double a [ 2…

​​​​​What is the output of the following C\text{C} program?

  1. #include <stdio.h>
  2. int main()  {
  3. double a[2]={20.0,25.0},* p,* q;
  4. p=a ;
  5. q=p+1 ;
  6. printf("%d,%d", (int) (q-p),( int)(* q- * p));
  7. return 0;}
  1. A
    4,8
  2. B
    1,5
    Correct answer
  3. C
    8,5
  4. D
    1,8

Answer

(B) 1 , 5 1,5 1 , 5

Approach

Track the pointer assignments, then evaluate the two integer-cast expressions: pointer subtraction counts array elements, while dereferenced-value subtraction uses the double values 25.0 and 20.0.

The program computes the pointer difference q-p, which is 1, and the integer-cast value difference (int)(*q-*p), which is (int)(25.0-20.0)=5. Thus the printed output is 1,5, making option B correct.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.