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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms