0

Please give me a proper solution of this program and also give the explanation how output will comes ?

#include<stdio.h> #include<string.h> #include<conio.h> int main() { char* c="GAMESOFTHRONE18"; char* p=c; printf("%d",(int) strlen (c+2[p]-6[p]-1)); return 0; } //Output is:9

16th Nov 2018, 1:25 PM
vishnu mhaske
vishnu mhaske - avatar
2 Answers
+ 4
"Then c is offset by 6 so strlen is passed "FTHRONE18" which contains 9 characters." I didn't realize that until printf("%s", (c + 2[p] - 6[p] - 1));
16th Nov 2018, 2:52 PM
Babak
Babak - avatar
+ 3
c and p contains the address to the beginning of the string "GAMESOFTHRONE18". 2[p] is identical to *(p+2), which in turn is the same as p[2], or the character at index 2, which is M. With the same logic 6[p] would then match the character F. The ascii value for M is 77 and 70 for F. 77 - 70 = 7 - 1 = 6. Then c is offset by 6 so strlen is passed "FTHRONE18" which contains 9 characters.
16th Nov 2018, 2:30 PM
Dennis
Dennis - avatar