std::strlen(3)
π NAME π
okay bestie, welcome to the std::strlen(3) page from the C++ Standard Libary manual π
. The name is literally std::strlen - std::strlen (yes, that's the whole NAME section, no cap). We're gonna get you comfy with it.
β¨ Synopsis β¨
Defined in header <cstring>:
std::size_t strlen( const char* str );
So std::strlen returns the length of the given byte string β that's the number of characters in a character array whose first element is pointed to by str, up to and not including the first null character π«Ά. The behavior is undefined if there is no null character in the character array pointed to by str π. Like, fr fr, do not let that happen.
πΈ Parameters πΈ
strβ pointer to the null-terminated byte string to be examined π
π Return value π
The length of the null-terminated string str β¨.
π¦ Possible implementation π¦
Here's the possible implementation, bestie β copy the shape, not my whole personality:
std::size_t strlen(const char* start)
{
// NB: start is not checked for nullptr!
const char* end = start;
while (*end != '\0')
++end;
return end - start;
}
π Example π
// Run this code
#include <cstring>
#include <iostream>
int main()
{
const char str[] = "dog cat\0mouse";
std::cout << "without null character: " << std::strlen(str) << '\n'
<< "with null character: " << sizeof str << '\n';
}
π» Output:
without null character: 7
with null character: 14
π See also π
wcslenβ returns the length of a wide string (function) πmblenβ returns the number of bytes in the next multibyte character (function) π§- C documentation for
strlenπ
Source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::strlen(3) π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms