strndup(3)

strndup3

Nonstring duplicate-into-string

Usage

#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

Library

Standard C library (libc, -lc)

Overview

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

InterfaceAttributeValue
strndup()Thread safetyMT-Safe

Releases

13 releases since Debian 3.0 (Woody); the text changed 12 times.

  1. 3.0
  2. 3.1
  3. 4.0
  4. 5.0
  5. 6.0
  6. 7
  7. 8
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. Arch

Standards

POSIX.1-2008.

History

POSIX.1-2008.

From man-pages 6.19-1 in Arch Linux 2026-11-01. Parsed by carta from the page's man source.