1. Home
  2. Computer Science MCQs
  3. C Programming
  4. What does strlen("hello") return in C?

Added 30 Nov -0001

What does strlen("hello") return in C?

  1. 5, the characters before the terminating null byte Correct answer
  2. 6, counting the terminating null byte as well
  3. 4, because the first character is not counted at all
  4. It is undefined for a string literal argument

Correct answer

A. 5, the characters before the terminating null byte

13 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs