+ 1

Why the answer is 3 ??

char a[20]; int x; x = strlen(a); printf("%d",x); O/P- 3

23rd Jul 2019, 6:13 PM
Suraj Patra
Suraj Patra - avatar
3 Answers
+ 2
You haven't written any letters into the string, you have only reserved the space for it. So what's in there, is totally unpredictable (and by extension the result of any string-related function as well).
23rd Jul 2019, 6:46 PM
HonFu
HonFu - avatar
+ 2
Thanks man
23rd Jul 2019, 6:54 PM
Suraj Patra
Suraj Patra - avatar
0
HonFu [#GoGetThatBugChamp!] In some other thread there was a similar situation were a n × n array was accessed outside its boundaries and always retuned Null. That lead me to the assumption that all C code is run in debug mode and all array are initially set Null during compilation. Wrong guess? Cheers.C
23rd Jul 2019, 8:52 PM
ChrA
ChrA - avatar