strndup - Man Page
nonstring duplicate-into-string
Library
Standard C library (libc, -lc)
Synopsis
#include <string.h> // see memory.h(3head)
char *strndup(size_t n;
const char strn[n], size_t n);Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
strndup():
Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_GNU_SOURCEDescription
This function duplicates at most n nonnull characters from the array pointed to by strn, followed by a null character, into new memory obtained with malloc(3).
Return Value
On success, the strndup() function returns a pointer to the duplicated string. It returns NULL if insufficient memory was available, with errno set to indicate the error.
Errors
See malloc(3).
Attributes
For an explanation of the terms used in this section, see attributes(7).
| Interface | Attribute | Value |
|---|---|---|
| strndup() | Thread safety | MT-Safe |
Standards
POSIX.1-2008.
History
POSIX.1-2008.
See Also
memory.h(3head), malloc(3), strndupa(3)
Info
2026-08-17 Linux man-pages 6.20