Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      strlen, strnlen_s

      From cppreference.com
      <c‎ |string‎ |byte
       
       
       
       
      Defined in header<string.h>
      size_t strlen(constchar* str);
      (1)
      size_t strnlen_s(constchar* 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 bystr up to and not including the first null character.
      The behavior is undefined ifstr is not a pointer to a null-terminated byte string.
      2) Same as(1), except that the function returns zero ifstr is a null pointer and returnsstrsz if the null character was not found in the firststrsz bytes ofstr.
      The behavior is undefined ifstr is not a pointer to a null-terminated byte string andstrsz is greater than the size of that character array.
      As with all bounds-checked functions,strnlen_s is 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 constant1 before including<string.h>.

      Contents

      [edit]Parameters

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

      [edit]Return value

      1) The length of the null-terminated byte stringstr.
      2) The length of the null-terminated byte stringstr on success, zero ifstr is a null pointer,strsz if the null character was not found.

      [edit]Notes

      strnlen_s andwcsnlen_s are the onlybounds-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.

      [edit]Example

      Run this code
      #define __STDC_WANT_LIB_EXT1__ 1#include <stdio.h>#include <string.h> int main(void){constchar 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: 45with null character:    46without null character: 45

      [edit]References

      • 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

      [edit]See also

      (C95)(C11)
      returns the length of a wide string
      (function)[edit]
      returns the number of bytes in the next multibyte character
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/string/byte/strlen&oldid=172378"

      [8]ページ先頭

      ©2009-2025 Movatter.jp