strndup(3)
β¨ strndup β the manual, but make it cute β¨
okay bestie, buckle up, we're learning strndup(3) from the Library Functions Manual and honestly? it's giving underrated icon π
πΈ NAME πΈ
strndup β nonstring duplicate-into-string</p> π
π LIBRARY π
the Standard C library (libc, link it with -lc) π«Ά
π¦ SYNOPSIS π¦
like this, no cap:
#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 (peep feature_test_macros(7) for the tea β):
strndup():
- Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L - Before glibc 2.10:
_GNU_SOURCE
π DESCRIPTION π
okay so here's the vibe: this function duplicates at most n nonnull characters from the array that strn is pointing at, and then it tacks a null character on the end π± β the whole thing lands in brand new memory that it got from malloc(3). new memory, who dis π
π RETURN VALUE π
on success, strndup() hands you back a pointer to the duplicated string β¨ it's slaying, it's iconic, it's your string now.
but if there wasn't enough memory available? it returns NULL π and sets errno to show you what went wrong. rude, but okay.
π ERRORS π
just go look at malloc(3), bestie. it's all there π«Ά
π ATTRIBUTES π
for the full explanation of the terms used in this section, see attributes(7) π
here's the rundown:
- Interface:
strndup() - Attribute: Thread safety
- Value:
MT-Safe
so yeah β MT-Safe, meaning it's chill to call from multiple threads π
β STANDARDS β
POSIX.1-2008. periodt π
π°οΈ HISTORY π°οΈ
also POSIX.1-2008. β yeah she's been around a minute, main character energy since day one π
π₯ SEE ALSO π₯
and that's the whole page, bestie!! this was Linux man-pages 6.20, dated 2026-08-17, and we were reading all about strndup(3) π¦β¨ go forth and duplicate responsibly, iykyk π
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms