What does strlen("hello") return in C?
Correct answer
A. 5, the characters before the terminating null byte
13 views
A. 5, the characters before the terminating null byte
13 views
strlen counts characters until it reaches the null terminator and does not include that terminator, so "hello" gives 5. sizeof on the same literal would give 6 because the array includes the hidden '\0'. This one-byte difference is the classic cause of buffer overflows when code allocates strlen(s) bytes and then copies s into it.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.