What is the output of the following C program? #include int main() { double…

2024

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;

}

  1. A.

    4,8

  2. B.

    1,5

  3. C.

    8,5

  4. D.

    1,8

Attempted by 159 students.

Show answer & explanation

Correct answer: B

Answer: 1,5

Explanation:

  • p = a sets p to point to the first element (20.0); q = p + 1 sets q to point to the second element (25.0).

  • Pointer subtraction q - p yields the number of elements between the pointers. Since q points to a[1] and p to a[0], q - p = 1.

  • Value subtraction *q - *p computes 25.0 - 20.0 = 5.0. Casting to int gives 5.

  • Therefore printf prints 1,5.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir