strncpy(3)

✨ strncpy(3) β€” nonstring copy ✨

okay bestie, welcome to the strncpy page πŸ’– lowkey the name is lying to you but we'll get to that. (This is from the Standard C library β€” libc, link with -lc.)


🌸 SYNOPSIS 🌸

#include <string.h>  // see memory.h(3head)

char *strncpy(size_t dsize;
              char dst[restrict dsize], const char *restrict src,
              size_t dsize);

yes the dsize really shows up twice, she's iconic like that πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so here's the vibe: this function copies all the non-null characters from the string that src points at, and drops them into the array that dst points at. Simple.

But ✨here's the twist✨ β€” if the source doesn't have enough non-null bytes to fill up the destination, she pads the rest with trailing null bytes. She will fill that whole buffer, no cap.

And if the destination buffer (limited by its size, bestie) isn't big enough to hold the copy? The resulting character sequence gets truncated. 😭

It's literally equivalent to:

stpncpy(dst, src, dsize), dst

🫢 RETURN VALUE 🫢

dst. that's it. that's the whole thing. she gives you back your own pointer like the main character she is πŸ‘‘


πŸ’œ ATTRIBUTES πŸ’œ

For what all these fancy attribute words actually mean, go peek at attributes(7).

  • strncpy() β€” Thread safety: MT-Safe ✨
ngl MT-Safe is the slay here, thread girlies you're fine πŸ’…

🌈 STANDARDS 🌈

  • C11
  • POSIX.1-2008

🧚 HISTORY 🧚

  • C89
  • POSIX.1-2001
  • SVr4
  • 4.3BSD

she's been on the block for a MINUTE, fr fr πŸŽ€


πŸ’€ CAVEATS πŸ’€

okay bestie this is the important part, listen up πŸ“’

The name of this function is confusing. Like, genuinely. This function produces a null-padded character sequence β€” it does not produce a string (go read string_copying(7) for the whole tea). For example:

strncpy(buf, "1", 5);       // { '1',   0,   0,   0,   0 }
strncpy(buf, "1234", 5);    // { '1', '2', '3', '4',   0 }
strncpy(buf, "12345", 5);   // { '1', '2', '3', '4', '5' }
strncpy(buf, "123456", 5);  // { '1', '2', '3', '4', '5' }

see how the last two look IDENTICAL? πŸ™ˆ yeah.

It is impossible to tell whether the call truncated something just by looking at the result of the call β€” it looks exactly the same as a character sequence that just barely fits the destination buffer. So if you wanna catch truncation, you have to compare the length of the input string with the size of the destination buffer yourself. no shortcuts, bestie. πŸ’…

⭐ EXAMPLES ⭐

This function is only intended to be used for writing to fixed-width null-padded character arrays β€” think members of utmp(5), that kind of thing.

strncpy(utmp->ut_user, "foo", countof(utmp->ut_user));

that's the whole assignment, periodt πŸ’–


🌸 SEE ALSO 🌸


and that's the page!! this one's from Linux man-pages 6.20, dated 2026-08-10, strncpy(3) β€” go forth and pad responsibly, king πŸ’…βœ¨πŸ‘‘

πŸ“¦ 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