1. Home
  2. Computer Science MCQs
  3. C Programming
  4. For char s[] = "hello"; what are the values of sizeof s…

Added 30 Nov -0001

For char s[] = "hello"; what are the values of sizeof s and strlen(s)?

  1. 5 and 5
  2. 5 and 6
  3. 6 and 6
  4. 6 and 5 Correct answer

Correct answer

D. 6 and 5

17 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? 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