2 .\" Copyright 2022 Alejandro Colomar <alx@kernel.org>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH strcpy 3 (date) "Linux man-pages (unreleased)"
8 stpcpy, strcpy, strcat \- copy or catenate a string
11 .RI ( libc ", " \-lc )
14 .B #include <string.h>
16 .BI "char *stpcpy(char *restrict " dst ", const char *restrict " src );
17 .BI "char *strcpy(char *restrict " dst ", const char *restrict " src );
18 .BI "char *strcat(char *restrict " dst ", const char *restrict " src );
22 Feature Test Macro Requirements for glibc (see
23 .BR feature_test_macros (7)):
29 _POSIX_C_SOURCE >= 200809L
38 These functions copy the string pointed to by
41 at the buffer pointed to by
43 The programmer is responsible for allocating a destination buffer large enough,
45 .IR "strlen(src) + 1" .
46 For the difference between the two functions, see RETURN VALUE.
49 This function catenates the string pointed to by
51 after the string pointed to by
53 (overwriting its terminating null byte).
54 The programmer is responsible for allocating a destination buffer large enough,
56 .IR "strlen(dst) + strlen(src) + 1" .
58 An implementation of these functions might be:
63 stpcpy(char *restrict dst, const char *restrict src)
67 p = mempcpy(dst, src, strlen(src));
74 strcpy(char *restrict dst, const char *restrict src)
81 strcat(char *restrict dst, const char *restrict src)
83 stpcpy(dst + strlen(dst), src);
92 a pointer to the terminating null byte of the copied string.
97 These functions return
100 For an explanation of the terms used in this section, see
108 Interface Attribute Value
113 T} Thread safety MT-Safe
126 POSIX.1-2001, POSIX.1-2008, C99, SVr4, 4.3BSD.
134 If the destination buffer is not large enough,
135 the behavior is undefined.
139 .BR feature_test_macros (7).
142 can be very inefficient.
144 .UR https://www.joelonsoftware.com/\:2001/12/11/\:back\-to\-basics/
148 .\" SRC BEGIN (strcpy.c)
163 maxsize = strlen("Hello ") + strlen("world") + strlen("!") + 1;
164 buf1 = malloc(sizeof(*buf1) * maxsize);
166 err(EXIT_FAILURE, "malloc()");
167 buf2 = malloc(sizeof(*buf2) * maxsize);
169 err(EXIT_FAILURE, "malloc()");
172 p = stpcpy(p, "Hello ");
173 p = stpcpy(p, "world");
177 printf("[len = %zu]: ", len);
178 puts(buf1); // "Hello world!"
181 strcpy(buf2, "Hello ");
182 strcat(buf2, "world");
186 printf("[len = %zu]: ", len);
187 puts(buf2); // "Hello world!"
198 .BR string_copying (7)