For char s[] = "hello"; what are the values of sizeof s and strlen(s)?
Correct answer
D. 6 and 5
17 views
D. 6 and 5
17 views
sizeof is an operator reporting the whole array size including the terminating null character, so it gives 6. strlen scans at run time until the null and returns the number of characters before it, so it gives 5. If s were a pointer, sizeof would give 8 on a 64-bit build while strlen would still give 5.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.