in Programming in C edited by
2,526 views
2 votes
2 votes

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

#include <stdio.h>
int main()  {
double $a[2]=\{20.0,25.0\},{ }^{*} \mathrm{p},{ }^{*} \mathrm{q}$;
$\mathrm{p}=\mathrm{a} ;$
$q=p+1 ;$
printf("%d,%d", (int) $(q-p),($ int $\left.)\left({ }^{*} q-{ }^{*} p\right)\right)$;
return $0$ ; }

  1. $4,8$
  2. $1,5$
  3. $8,5$
  4. $1,8$
in Programming in C edited by
by
2.5k views

1 comment

edited by
Ans B
0
0

2 Answers

0 votes
0 votes

$\text{int(q-p)}$ is pointer subtraction which subtracts the address of the pointer whereas $\text{int(*q-*p)}$ is also performs subtraction operation but it subtracts the values which are hold by pointer $*p,*q$

In the printf statement, the format specifier is $\%d$ which returns output in an integer from

So

 $\text{int(q-p)} = \frac{\text{address of q} - \text{address of p}}{\text{sizeof(pointer))}} = \frac{2008-2000}{8}=\frac{8}{8}=1$

$\text{int(*q-*p)=int(25.0-20.0)=int(5.0)=5}$

Option $(B)$ is correct.

4 Comments

Pointer address subtraction is nothing but the distance between variables that are subtracted. if we are not divided by the pointer size, then the result is incorrect as it gives $5$.
1
1
Understood, Thanks!
0
0
I think in the denominator it should be "sizeof(double)".
0
0
0 votes
0 votes
options B is correct
Answer:

Related questions