summaryrefslogtreecommitdiff
path: root/devdocs/c/string%2Fbyte%2Fstrlen.html
blob: e8a60fbcf3ca8bc118530b314c78d41c0d695fea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    <h1 id="firstHeading" class="firstHeading">strlen, strnlen_s</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;string.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td> <pre data-language="c">size_t strlen( const char *str );</pre>
</td> <td> (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">size_t strnlen_s( const char *str, size_t strsz );</pre>
</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr>  </table> <div class="t-li1">
<span class="t-li">1)</span> Returns the length of the given null-terminated byte string, that is, the number of characters in a character array whose first element is pointed to by <code>str</code> up to and not including the first null character.</div> <div class="t-li1">
 The behavior is undefined if <code>str</code> is not a pointer to a null-terminated byte string.</div> <div class="t-li1">
<span class="t-li">2)</span> Same as <span class="t-v">(1)</span>, except that the function returns zero if <code>str</code> is a null pointer and returns <code>strsz</code> if the null character was not found in the first <code>strsz</code> bytes of <code>str</code>.</div> <div class="t-li1">
 The behavior is undefined if both <code>str</code> points to a character array which lacks the null character and the size of that character array &lt; <code>strsz</code>; in other words, an erroneous value of <code>strsz</code> does not expose the impending buffer overflow. As with all bounds-checked functions, <code>strnlen_s</code> only guaranteed to be available if <code>__STDC_LIB_EXT1__</code> is defined by the implementation and if the user defines <code>__STDC_WANT_LIB_EXT1__</code> to the integer constant <code>1</code> before including <a href="../byte" title="c/string/byte"><code>&lt;string.h&gt;</code></a>.</div>  <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> str </td> <td> - </td> <td> pointer to the null-terminated byte string to be examined </td>
</tr> <tr class="t-par"> <td> strsz </td> <td> - </td> <td> maximum number of characters to examine </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <div class="t-li1">
<span class="t-li">1)</span> The length of the null-terminated byte string <code>str</code>.</div> <div class="t-li1">
<span class="t-li">2)</span> The length of the null-terminated byte string <code>str</code> on success, zero if <code>str</code> is a null pointer, <code>strsz</code> if the null character was not found.</div> <h3 id="Notes"> Notes</h3> <p><code>strnlen_s</code> and <code>wcsnlen_s</code> are the only <a href="../../error" title="c/error">bounds-checked functions</a> that do not invoke the runtime constraints handler. They are pure utility functions used to provide limited support for non-null terminated strings.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#define __STDC_WANT_LIB_EXT1__ 1
#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
 
int main(void)
{
    const char str[] = "How many characters does this string contain?";
 
    printf("without null character: %zu\n", strlen(str));
    printf("with null character:    %zu\n", sizeof str);
 
#ifdef __STDC_LIB_EXT1__
    printf("without null character: %zu\n", strnlen_s(str, sizeof str));
#endif
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">without null character: 45
with null character:    46
without null character: 45</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul>
<li> 7.24.6.3 The strlen function (p: 372) </li>
<li> K.3.7.4.4 The strnlen_s function (p: 623) </li>
</ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.21.6.3 The strlen function (p: 334) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.11.6.3 The strlen function </li></ul>
</ul>          <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="../wide/wcslen" title="c/string/wide/wcslen"> <span class="t-lines"><span>wcslen</span><span>wcsnlen_s</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> returns the length of a wide string <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../multibyte/mblen" title="c/string/multibyte/mblen"> <span class="t-lines"><span>mblen</span></span></a></div> </td> <td> returns the number of bytes in the next multibyte character <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/byte/strlen" title="cpp/string/byte/strlen">C++ documentation</a></span> for <code>strlen</code> </td>
</tr> </table>            <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/string/byte/strlen" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strlen</a>
  </p>
</div>