Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3403058
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:12:50+00:00 2026-05-18T05:12:50+00:00

How does strlen() work internally? Are there any inherent bugs in the function?

  • 0

How does strlen() work internally? Are there any inherent bugs in the function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:12:51+00:00Added an answer on May 18, 2026 at 5:12 am

    strlen usually works by counting the characters in a string until a \0 character is found. A canonical implementation would be:

    size_t strlen (char *str) {
        size_t len = 0;
        while (*str != '\0') {
            str++;
            len++;
        }
        return len;
    }
    

    As for possible inherent bugs in the function, there are none – it works exactly as documented. That’s not to say it doesn’t have certain problems, to wit:

    • if you pass it a "string" that doesn’t have a \0 at the end, you may run into problems but technically, that’s not a C string (a) and it’s your own fault.
    • you can’t put \0 characters within your string but, again, it wouldn’t be a C string in that case.
    • it’s not the most efficient way – you could store a length up front so you could get the length much quicker.

    But none of these are bugs, they’re just consequences of a design decision.

    On that last bullet point, see also this excellent article by Joel Spolsky where he discusses various string formats and their characteristics, including normal C strings (with a terminator), Pascal strings (with a length) and the combination of the two, null terminated Pascal strings.

    Though he has a more, shall we say, "colorful" term for that final type, one which frequently comes to mind whenever I thing of Python’s excellent (and totally unrelated) f-strings 🙂


    (a) A C string is defined as a series of non-terminator characters (any character other than \0) followed by a terminator. Hence this definition disallows both embedded terminators within the sequence, and sequences without such a terminator. Or, putting it more succinctly (as per the ISO C standard):

    A string is a contiguous sequence of characters terminated by and including the first null character.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Guile 1.6.*, the function scm_istring2number(char *str,int strlen,int radix) does the work. However, this
Does anybody know any good resources for learning how to program CIL with in-depth
Does anyone know how to get IntelliSense to work reliably when working in C/C++
Does anyone have any recommendations of tools that can be of assistance with moving
Does Google force employees who have offers from Facebook to leave immediately?
Does anyone remember the XMP tag? What was it used for and why was
Does the Java language have delegate features, similar to how C# has support for
Does anyone use have a good regex library that they like to use? Most
Does anyone know how to transform a enum value to a human readable value?
Does C# have built-in support for parsing strings of page numbers? By page numbers,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.