| Defined in header
|
||
| size_t strlen( const char* str ); |
(1) | |
| (2) | (since C11) | |
str up to and not including the first null character.str is not a pointer to a null-terminated byte string.str is a null pointer and returns strsz if the null character was not found in the first strsz bytes of str.str is not a pointer to a null-terminated byte string and strsz is greater than the size of that character array.
- As with all bounds-checked functions,
strnlen_sis only guaranteed to be available if__STDC_LIB_EXT1__is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__to the integer constant1before including<string.h>.
Parameters
| str | - | pointer to the null-terminated byte string to be examined |
| strsz | - | maximum number of characters to examine |
Return value
str.str on success, zero if str is a null pointer, strsz if the null character was not found.Notes
strnlen_s and wcsnlen_s are the only bounds-checked functions that do not invoke the runtime constraints handler. They are pure utility functions used to provide limited support for non-null terminated strings.
Example
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <string.h>
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
}without null character: 45
with null character: 46
without null character: 45References
- C23 standard (ISO/IEC 9899:2024):
- 7.24.6.3 The strlen function (p: TBD)
- K.3.7.4.4 The strnlen_s function (p: TBD)
- C17 standard (ISO/IEC 9899:2018):
- 7.24.6.3 The strlen function (p: TBD)
- K.3.7.4.4 The strnlen_s function (p: TBD)
- C11 standard (ISO/IEC 9899:2011):
- 7.24.6.3 The strlen function (p: 372)
- K.3.7.4.4 The strnlen_s function (p: 623)
- C99 standard (ISO/IEC 9899:1999):
- 7.21.6.3 The strlen function (p: 334)
- C89/C90 standard (ISO/IEC 9899:1990):
- 4.11.6.3 The strlen function
See also
| (C95)(C11) |
returns the length of a wide string (function) |
| returns the number of bytes in the next multibyte character (function) | |
| C++ documentation for strlen
| |
Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.