strcpy(3) — Linux manual page

NAME | LIBRARY | SYNOPSIS | DESCRIPTION | RETURN VALUE | ATTRIBUTES | STANDARDS | HISTORY | CAVEATS | EXAMPLES | SEE ALSO | COLOPHON

strcpy(3)                Library Functions Manual               strcpy(3)

NAME         top

       strcpy, strcat - copy or catenate a string

LIBRARY         top

       Standard C library (libc, -lc)

SYNOPSIS         top

       #include <string.h>

       char *strcpy(char *restrict dst, const char *restrict src);
       char *strcat(char *restrict dst, const char *restrict src);

DESCRIPTION         top

       strcpy()
              This function copies the string pointed to by src, into a
              string at the buffer pointed to by dst.  The programmer is
              responsible for allocating a destination buffer large
              enough, that is, strlen(src) + 1.

              It is equivalent to

                  stpcpy(dst, src), dst

       strcat()
              This function catenates the string pointed to by src, after
              the string pointed to by dst (overwriting its terminating
              null byte).  The programmer is responsible for allocating a
              destination buffer large enough, that is, strlen(dst) +
              strlen(src) + 1.

              It is equivalent to

                  stpcpy(strnul(dst), src), dst

RETURN VALUE         top

       These functions return dst.

ATTRIBUTES         top

       For an explanation of the terms used in this section, see
       attributes(7).
       ┌──────────────────────────────────────┬───────────────┬─────────┐
       │ Interface                            Attribute     Value   │
       ├──────────────────────────────────────┼───────────────┼─────────┤
       │ strcpy(), strcat()                   │ Thread safety │ MT-Safe │
       └──────────────────────────────────────┴───────────────┴─────────┘

STANDARDS         top

       C11, POSIX.1-2008.

HISTORY         top

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

CAVEATS         top

       The strings src and dst may not overlap.

       If the destination buffer is not large enough, the behavior is
       undefined.  See _FORTIFY_SOURCE in feature_test_macros(7).

       strcat() can be very inefficient.  Read about Shlemiel the painter
       ⟨https://www.joelonsoftware.com/2001/12/11/back-to-basics/⟩.

EXAMPLES         top

       #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 ALSO         top

       stpcpy(3), strdup(3), string(3), wcscpy(3), string_copying(7)

COLOPHON         top

       This page is part of the man-pages (Linux kernel and C library
       user-space interface documentation) project.  Information about
       the project can be found at 
       ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report
       for this manual page, see
       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.
       This page was obtained from the tarball man-pages-6.18.tar.gz
       fetched from
       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on
       2026-05-24.  If you discover any rendering problems in this HTML
       version of the page, or you believe there is a better or more up-
       to-date source for the page, or you have corrections or
       improvements to the information in this COLOPHON (which is not
       part of the original manual page), send a mail to
       man-pages@man7.org

Linux man-pages 6.18            2026-02-25                      strcpy(3)

Pages that refer to this page: bcopy(3)memccpy(3)memcpy(3)memmove(3)stpcpy(3)string(3)wcpcpy(3)wcscat(3)wcscpy(3)feature_test_macros(7)signal-safety(7)string_copying(7)