strlen, strnlen, strnlen_s - string length operations
#include <string.h> size_t strlen(const char *s);
size_t strnlen(const char *s, size_t n);
#define __STDC_WANT_LIB_EXT1__ 1 #include <string.h> size_t strnlen_s(const char *s, size_t maxsize);
These functions calculate the length in bytes of strings (arrays of characters). Depending on the function, strings may be either terminated by a null character or bounded by a maximum length of n bytes. They are designed for use with encodings representing each character as a single byte, and all character counts are measured in individual bytes, even if a string with multibyte characters is used. They do not check for null pointers, and programs may crash if passing null or otherwise invalid pointers to these functions.
The strlen() function returns the number of bytes in s, not including the terminating null character.
The strnlen() function returns the smaller of n or the number of bytes in s, not including the terminating null character. The strnlen() function never examines more than n bytes of the string pointed to by s.
The strnlen_s() function is part of the C11 bounds checking interfaces specified in the C11 standard, Annex K. Each of these functions provides similar functionality to their respective non-bounds checking counterpart functions, but with additional safety checks in the form of explicit runtime constraints as defined in the C11 standard. See runtime_constraint_handler(3C) and INCITS/ISO/IEC 9899:2011.
The strnlen_s() function returns zero if s is a null pointer. Otherwise, the number of bytes preceding the terminating null character is returned. If there is no terminating null character in the first maxsize characters pointed to by s, strnlen_s() returns maxsize. There are no defined error conditions in which strnlen_s() will invoke the runtime constraint handler.
See attributes(7) for descriptions of the following attributes:
See standards(7) for descriptions of the following standards:
The strlen_s() function was added to Oracle Solaris in the Solaris 11.4.0 release.
The strnlen() function was added to Oracle Solaris in the Solaris 11.0.0 release.
The strlen() function has been included in all Sun and Oracle releases of Solaris.