Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Wednesday, July 27, 2022
 
 

strlen(3C)

Name

strlen, strnlen, strnlen_s - string length operations

Synopsis

#include <string.h>

size_t strlen(const char *s);
size_t strnlen(const char *s, size_t n);

C11 Bounds Checking Interface

#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>

size_t strnlen_s(const char *s, size_t maxsize);

Description

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.

strlen(), strnlen()

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.

C11 Bounds Checking Interface

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.

Attributes

See attributes(7) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
MT-Level
Async-Signal-Safe
Standard
See below

Standard

See standards(7) for descriptions of the following standards:

INTERFACES
APPLICABLE STANDARDS
strlen()
  • C89 through C11,
  • POSIX.1-1990 through 2008,
  • SUS through SUSv4,
  • XPG1 through XPG7
strnlen()
  • POSIX.1-2008,
  • SUSv4,
  • XPG7
strlen_s()
C11 Annex K

See Also

string(3C), wcslen(3C), attributes(7), standards(7)

History

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.