In C, what do sizeof("abc") and strlen("abc") evaluate to?
A
4 and 3, because sizeof counts the terminating NUL while strlen does not
Correct answer
B
3 and 3, because both operators measure only the visible characters
C
3 and 4, because sizeof excludes the NUL that strlen includes in its count
D
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
⚔️
Challenge a friend on C Programming
Same MCQs, timed, live leaderboard. Send the link on WhatsApp – no sign-up needed.
Create challenge
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
Doubts & discussionNot sure why the answer is A? Ask here and get an explanation.
Ask a doubt about this MCQ
‹ Previous MCQ A buffer's contents must be shifted three bytes to the right inside the same array. Why is memmove required instead of memcpy? Next MCQ › A variable must hold a pointer to a function taking an int and returning int. Which declaration is right, and how does it compare with the alternative?