Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>strlen</code> usually works by counting the characters in a string until a <code>\0</code> character is found. A canonical implementation would be:</p> <pre><code>size_t strlen (char *str) { size_t len = 0; while (*str != '\0') { str++; len++; } return len; } </code></pre> <p>There is no inherent bug in the function, it works exactly as documented.</p> <p>That's not to say it doesn't have problems, to wit:</p> <ul> <li>if you pass it a "string" that doesn't have a <code>\0</code> at the end, you may run into problems but technically, that's not a C string <sup>(a)</sup> and it's your own fault.</li> <li>you can't put <code>\0</code> characters in your string but, again, it wouldn't be a C string in that case.</li> <li>it's not the most efficient way - you could store a length up front so you could get the length much quicker.</li> </ul> <p>But none of those are bugs, they're just consequences of a design decision.</p> <p>See also <a href="http://www.joelonsoftware.com/articles/fog0000000319.html">this excellent article</a> by Joel Spolsky where he discusses various string formats and their characteristics, including normal C strings, Pascal strings and the combination of the two, null terminated Pascal strings, though he has a more, shall we say, "colorful" term for them :-)</p> <hr> <p><sup>(a)</sup> A C string is defined as a series of non-terminator characters (ie, any other than <code>\0</code>) followed by that terminator. Hence this definition disallows both embedded terminators within the sequence and sequences without such a terminator.</p> <p>Or, putting it more succinctly (as per the ISO standard):</p> <blockquote> <p>A string is a contiguous sequence of characters terminated by and including the first null character.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload