Skip to content
Official PYQ · 2024

GATE CS 2024 Set 2 Q26 — Programming in C

Explain with AI

​​​​​What is the output of the following C\text{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;} 
Choose one option
View worked solution

Answer: B

Let us trace the execution of the program step-by-step:

  1. Array Initialization:

    An array a of two double elements is initialized as:

    a[0] = 20.0 and a[1] = 25.0

  2. Pointer Assignments:
    • p = a; makes p point to the first element a[0].
    • q = p + 1; makes q point to the next element a[1].
  3. Evaluating the Expressions in printf:
    • First Expression (int)(q - p):
      In C, pointer subtraction (q - p) yields the distance between the two pointers in terms of the number of elements of the base type (here, double), not the number of bytes. Since q points to index 1 and p points to index 0: q−p=1\text{q} - \text{p} = 1 Casting this to int gives 1.
    • Second Expression (int)(*q - *p):
      Dereferencing the pointers gives their respective values: ∗q=25.0and∗p=20.0*\text{q} = 25.0 \quad \text{and} \quad *\text{p} = 20.0 Subtracting these values: ∗q−∗p=25.0−20.0=5.0*\text{q} - *\text{p} = 25.0 - 20.0 = 5.0 Casting 5.0 to int gives 5.

Thus, the printf statement outputs 1,5, which corresponds to option B.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions