In C, basic strlen on a digit-only literal: what is printed? #include<stdio.h> #include<string.h> int main() { printf("%d ", strlen("123456")); return 0; }

Difficulty: Easy

Correct Answer: 6

Explanation:


Introduction / Context:
This is a direct application of strlen to a string literal. strlen counts characters up to but not including the first null terminator.



Given Data / Assumptions:

  • The literal is "123456".
  • No embedded nulls are present.


Concept / Approach:
strlen("abcdef") returns the number of visible characters before the implicit '\0'. Here, that count is the number of digits in the literal.



Step-by-Step Solution:
List characters: 1 2 3 4 5 6 → 6 characters.strlen returns 6, which printf prints as 6.


Verification / Alternative check:
Compare with sizeof "123456" which would be 7 (adds the terminating null). This highlights the difference between strlen (logical length) and sizeof (storage in bytes for a literal).



Why Other Options Are Wrong:
(12) doubles the count; (7) confuses with sizeof. (2) and (0) have no basis here.



Common Pitfalls:
Confusing strlen with sizeof for string literals.



Final Answer:
6

More Questions from Strings

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion