STRNCPY(3P)
π PROLOG π
okay bestie, real talk before we even start: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ β so if you're on Linux, go peek at the corresponding Linux manual page for the tea on how Linux actually behaves. or, plot twist, the interface might not even be implemented on Linux at all. π
β¨ NAME β¨
stpncpy, strncpy β copy a fixed-length string and hand you back a pointer to the end of the array. yes, the end. it's giving "here's where it stops" energy. π
π SYNOPSIS π
#include <string.h>
char *stpncpy(char *restrict s1, const char *restrict s2, size_t n);
char *strncpy(char *restrict s1, const char *restrict s2, size_t n);
πΈ DESCRIPTION πΈ
so for strncpy() specifically: everything described on this reference page is aligned with the ISO C standard. if anything here ever beefs with the ISO C standard, that conflict was not intentional, okay? this volume of POSIX.1β2017 fully defers to the ISO C standard. the ISO C standard is the main character here, fr fr. π
the stpncpy() and strncpy() functions shall copy not more than n bytes from the array pointed to by s2 to the array pointed to by s1. and bestie, listen: bytes that follow a NUL character are not copied. they're just not invited to the party. π«
if the array pointed to by s2 is a string that's shorter than n bytes, then NUL characters shall be appended to the copy in the array pointed to by s1 β padding it out until n bytes in all are written. it's filling the whole seat row, no gaps. β¨
β οΈ and if copying takes place between objects that overlap? the behavior is undefined. that's not a vibe. do not do that. π
π«Ά RETURN VALUE π«Ά
if a NUL character gets written to the destination, then stpncpy() shall return the address of the first such NUL character. iconic. otherwise, it shall return &s1[n].
the strncpy() function shall return s1. simple. unbothered. consistent.
and no return values are reserved to indicate an error. so like, don't go reading the return value looking for drama, there isn't any. π
π ERRORS π
no errors are defined. not a single one. she's flawless, she has no notes. π
the following sections are informative β so everything after this point is bonus content, not requirements. iykyk. π¦
π EXAMPLES π
none. absolutely none. we're not doing examples today bestie. π
π APPLICATION USAGE π
okay this part actually matters, so pay attention:
- applications must provide the space in
s1for thenbytes to be transferred. that's on you, not on the function. she's not gonna allocate for you. π«Ά - also make sure the
s2ands1arrays do not overlap. like, at all.
character movement is performed differently in different implementations. so overlapping moves may yield surprises. and by "surprises" i mean the bad kind. not the cute kind. π
and here's the big one: if there is no NUL character byte in the first n bytes of the array pointed to by s2, the result is not null-terminated. like. at all. that's just how it is. no cap. π
π§ RATIONALE π§
none. she said "no explanation needed." periodt. π
β FUTURE DIRECTIONS β
none. the future is bright and empty and slay. π
π₯ SEE ALSO π₯
strcpy()wcsncpy()
and also, from the Base Definitions volume of POSIX.1β2017: <string.h>. go read it, bestie, it's good for you. π¦
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie π this one came to you from IEEE/The Open Group, dated 2017 β go off and copy those strings safely, no overlapping, mwah πβ¨
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms