+ 8

why it outputs 9 ?. Please explain this

char *s="a string"; char *p=s; while(*p++); printf("%d",(int)(p-s)); output: 9

10th Aug 2019, 11:04 AM
Sujithra
7 Respuestas
+ 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
10th Aug 2019, 11:55 AM
Prathvi
Prathvi - avatar
+ 8
Abhishek Jain thnq so much for explaining it...☺️
10th Aug 2019, 11:57 AM
Sujithra
+ 7
Abhishek Jain well explanation ..👍🏻
12th Aug 2019, 4:17 AM
Ayushi Gujarati
Ayushi Gujarati - avatar
+ 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.
10th Aug 2019, 10:28 PM
Sonic
Sonic - avatar
11th Aug 2019, 3:39 PM
Sujithra
+ 3
Nice example of low level programming language. Pointers and operations of bits are the most difficult topic in C. Thanks for recalling ;)
11th Aug 2019, 8:40 PM
Kamil Orłow
Kamil Orłow - avatar
+ 2
This code doesn't work. The while loop has no body.
11th Aug 2019, 1:24 PM
Loeschzwerg