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;}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 - pis 1, even though adoubleusually takes 8 bytes. *q - *psubtracts the stored values: \(25.0 - 20.0 = 5.0\), which the cast turns into theint5.
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