strcpy(3)
โจ strcpy โ the girlies' guide to string copy/catenate โจ
okay bestie, welcome to the strcpy(3) page ๐ this is strcpy and strcat from the Linux man-pages, and yes we're doing the whole thing, no skips, no cap.
๐ธ NAME ๐ธ
strcpy, strcat โ string copy/catenate
๐ LIBRARY ๐
Standard C library (libc, -lc) โ she lives in libc, that's her home ๐
๐ฅ SYNOPSIS ๐ฅ
#include <string.h>
char *strcpy(char *restrict dst, const char *restrict src);
char *strcat(char *restrict dst, const char *restrict src);
๐ DESCRIPTION ๐
strcpy()
okay so this one copies the string pointed to by src into a string at the buffer pointed to by dst. ๐ซถ
and here's the part everyone ignores and then cries about later: the programmer is responsible for allocating a destination buffer large enough, that is, strlen(src) + 1. that + 1 is for the null byte bestie, do not forget the null byte ๐ญ
it is equivalent to:
stpcpy(dst, src), dst
strcat()
this one catenates the string pointed to by src after the string pointed to by dst (overwriting its terminating null byte). so it's like, scooting over and letting src move in โจ
again, the programmer is responsible for allocating a destination buffer large enough, that is, strlen(dst) + strlen(src) + 1. yes you need both lengths. yes you need the + 1. no you cannot skip it ๐
it is equivalent to:
stpcpy(strnul(dst), src), dst
๐ RETURN VALUE ๐
these functions return dst. that's it, that's the whole return value. she gives you back your own pointer, iconic behaviour honestly ๐
๐ฆ ATTRIBUTES ๐ฆ
for an explanation of the terms used in this section, see attributes(7).
- Interface:
strcpy(),strcat() - Attribute: Thread safety
- Value:
MT-Safe
so like, she's MT-Safe, meaning the girlies can run her from multiple threads and she won't start drama ๐
โญ STANDARDS โญ
C11, POSIX.1-2008.
๐ HISTORY ๐
POSIX.1-2001, C89, SVr4, 4.3BSD. she's been around, she's a legacy queen ๐
๐ CAVEATS ๐
โ ๏ธ the stringssrcanddstmay not overlap. not even a little. do not do it.
โ ๏ธ if the destination buffer is not large enough, the behavior is undefined. that's UB bestie, that's the scary one, that's the one that gives you a segfault at 3am with no stack trace ๐ญ see_FORTIFY_SOURCEinfeature_test_macros(7)if you want the compiler to at least side-eye you about it.
๐ EXAMPLES ๐
here's the whole girl, verbatim, every line ๐ซถ
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void)
{
char *buf1;
size_t len, size;
size = strlen("Hello ") + strlen("world") + strlen("!") + 1;
buf1 = malloc(sizeof(*buf1) * size);
if (buf1 == NULL)
err(EXIT_FAILURE, "malloc()");
strcpy(buf1, "Hello ");
strcat(buf1, "world");
strcat(buf1, "!");
len = strlen(buf1);
printf("[len = %zu]: ", len);
puts(buf1); // "Hello world!"
free(buf1);
exit(EXIT_SUCCESS);
}
see how she computed size with all the strlens plus the + 1 and then malloc'd exactly that much? that's the whole vibe check in one code block. ate. ๐ฅ
๐ SEE ALSO ๐
stpcpy(3), strdup(3), string(3), wcscpy(3), string_copying(7)
that's the page bestie, go forth and always allocate enough bytes ๐โจ๐
this was fun โ Linux man-pages 6.20, dated 2026-08-03, mwah ๐
๐ฆ 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