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 strings src and dst may 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_SOURCE in feature_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