---
name: "strndup"
section: "3"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/strndup"
---

# strndup(3)

nonstring duplicate-into-string

## NAME

strndup - 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)](/feature_test_macros/7.md)):

**strndup**():

```
    Since glibc 2.10:
        _POSIX_C_SOURCE >= 200809L
    Before glibc 2.10:
        _GNU_SOURCE
```

## DESCRIPTION

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)](/malloc/3.md).

## 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)](/malloc/3.md).

## ATTRIBUTES

For an explanation of the terms used in this section, see [**attributes**(7)](/attributes/7.md).

| | | |
| --- | --- | --- |
| **Interface** | **Attribute** | **Value** |
| **strndup**() | Thread safety | MT-Safe |

## STANDARDS

POSIX.1-2008.

## HISTORY

POSIX.1-2008.

## SEE ALSO

[**memory.h**(3head)](/memory.h/3head.md), [**malloc**(3)](/malloc/3.md), [**strndupa**(3)](/strndupa/3.md)
