+ 8
why it outputs 9 ?. Please explain this
char *s="a string"; char *p=s; while(*p++); printf("%d",(int)(p-s)); output: 9
7 Answers
+ 9
Address of s like 4206640
than Address of p = 4206640
Address of p(while); its run 8 time because string length is 8 , and pointer p is incremented like
4206641
4206642
4206643
4206644
4206645
4206646
4206647
4206648
4206649(condition is wrong in loop)
4206649-4206640=9
+ 8
Abhishek Jain thnq so much for explaining it...âșïž
+ 7
Abhishek Jain well explanation ..đđ»
+ 5
Good explanation Abhishek Jain . Just have to point out that the specific address you used is an example only. Each time you run the program this address is most likely different but the increments and differences in address remain the same.
+ 3
Nice example of low level programming language. Pointers and operations of bits are the most difficult topic in C. Thanks for recalling ;)
+ 2
This code doesn't work. The while loop has no body.