1. Home
  2. Computer Science MCQs
  3. C Programming
  4. In C, what do sizeof("abc") and strlen("abc") evaluate to?

Added 30 Nov -0001

In C, what do sizeof("abc") and strlen("abc") evaluate to?

  1. 4 and 3, because sizeof counts the terminating NUL while strlen does not Correct answer
  2. 3 and 3, because both operators measure only the visible characters
  3. 3 and 4, because sizeof excludes the NUL that strlen includes in its count
  4. 4 and 4, because strlen also counts the implicit terminating NUL byte

Correct answer

A. 4 and 3, because sizeof counts the terminating NUL while strlen does not

14 views

MCQSEXAM

Explanation

sizeof applied to a string literal gives the array size including the terminating NUL, so "abc" occupies 4 bytes, whereas strlen stops at that NUL and returns 3. The two diverge further when a char array is declared with a larger bound than its initialiser. A classic trap is sizeof(pointer) where sizeof(array) is expected, which yields 8 on LP64 targets.

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