From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/string%2Fbyte%2Fstrlen.html | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 devdocs/c/string%2Fbyte%2Fstrlen.html (limited to 'devdocs/c/string%2Fbyte%2Fstrlen.html') diff --git a/devdocs/c/string%2Fbyte%2Fstrlen.html b/devdocs/c/string%2Fbyte%2Fstrlen.html new file mode 100644 index 00000000..e8a60fbc --- /dev/null +++ b/devdocs/c/string%2Fbyte%2Fstrlen.html @@ -0,0 +1,49 @@ +

strlen, strnlen_s

Defined in header <string.h>
size_t strlen( const char *str );
+
(1)
size_t strnlen_s( const char *str, size_t strsz );
+
(2) (since C11)
+1) 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 str up to and not including the first null character.
+ The behavior is undefined if str is not a pointer to a null-terminated byte string.
+2) Same as (1), except that the function returns zero if str is a null pointer and returns strsz if the null character was not found in the first strsz bytes of str.
+ The behavior is undefined if both str points to a character array which lacks the null character and the size of that character array < strsz; in other words, an erroneous value of strsz does not expose the impending buffer overflow. As with all bounds-checked functions, strnlen_s 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 constant 1 before including <string.h>.

Parameters

+ + +
str - pointer to the null-terminated byte string to be examined
strsz - maximum number of characters to examine

Return value

+1) The length of the null-terminated byte string str.
+2) The length of the null-terminated byte string 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 <string.h>
+#include <stdio.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
+}

Possible output:

+
without null character: 45
+with null character:    46
+without null character: 45

References

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
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/string/byte/strlen +

+
-- cgit v1.2.3