What will be output of following program ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What will be output of following program ?

int main() { int a=5,b=10,c; int *p=&a, *q=&b; c=p-q; printf("%d",c); return 0; } what is the ans of above program

17th Aug 2017, 8:10 AM
Shivshankar Gupta
Shivshankar Gupta - avatar
13 Answers
- 1
good to get to know that brother thanks ..✌
17th Aug 2017, 9:47 AM
sayan chandra
sayan chandra - avatar
+ 6
C is 3 in VC++ ide. It just tell you that a and b are 3 memory blocks apart. It is kind of platform dependant.
17th Aug 2017, 8:39 AM
Babak
Babak - avatar
+ 6
Dear Shivshankar it is all about memory offset.
17th Aug 2017, 8:42 AM
Babak
Babak - avatar
+ 1
@sayan pointers don't work like normal integers, so you can't add them. you can only add an int to then. subtraction of two pointers will give you the number of items (int In this case) in between them, instead of the difference of the raw pointer values.
17th Aug 2017, 9:43 AM
Jared Bird
Jared Bird - avatar
0
what is the outpur of above program
17th Aug 2017, 8:14 AM
Shivshankar Gupta
Shivshankar Gupta - avatar
0
out put will be 1...
17th Aug 2017, 8:38 AM
sayan chandra
sayan chandra - avatar
0
#sayan chandra how we get to 1 plzz comment fast
17th Aug 2017, 8:40 AM
Shivshankar Gupta
Shivshankar Gupta - avatar
0
ok babak sheykhan
17th Aug 2017, 8:44 AM
Shivshankar Gupta
Shivshankar Gupta - avatar
0
tysm sayan chandra
17th Aug 2017, 8:45 AM
Shivshankar Gupta
Shivshankar Gupta - avatar
0
The answer is undefined. It depends on the compiler used. Will probably be 1 (or -1) but not necessarily.
17th Aug 2017, 8:55 AM
Jared Bird
Jared Bird - avatar
- 1
the output is 1...showing... for any values of a,b
17th Aug 2017, 8:40 AM
sayan chandra
sayan chandra - avatar
- 1
the thing is.. its showing as &a=-12 &b=-14 p=-12 and q=-14 but c =1
17th Aug 2017, 8:44 AM
sayan chandra
sayan chandra - avatar
- 1
but out put of p and q is -12 & -14 c=p-q how is c=1 ??? and yes...printing c as q-p it results -1 BUT c=p+q shows invalid pointer addition
17th Aug 2017, 9:01 AM
sayan chandra
sayan chandra - avatar