strndup(3) Library Functions Manual strndup(3)

strndup - nonstring duplicate-into-string

Standard C library (libc-lc)

#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_SOURCE

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).

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.

See malloc(3).

For an explanation of the terms used in this section, see attributes(7).

Interface Attribute Value
strndup () Thread safety MT-Safe

POSIX.1-2008.

POSIX.1-2008.

memory.h(3head), malloc(3), strndupa(3)

2026-08-17 Linux man-pages 6.19