What is the output of the following 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;}
GATE CSE 2024Programming in C › Pointers, Arrays & StringsVerified
(B) 1 , 5 1,5 1 , 5
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.