Debian Codegraph @

upstream/debian-unstable/man3/strcpy.3

raw
1 '\" t
2 .\" Copyright, the authors of the Linux man-pages project
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH strcpy 3 2026-02-25 "Linux man-pages 6.18"
7 .SH NAME
8 strcpy, strcat \- copy or catenate a string
9 .SH LIBRARY
10 Standard C library
11 .RI ( libc ,\~ \-lc )
12 .SH SYNOPSIS
13 .nf
14 .B #include <string.h>
15 .P
16 .BI "char *strcpy(char *restrict " dst ", const char *restrict " src );
17 .BI "char *strcat(char *restrict " dst ", const char *restrict " src );
18 .fi
19 .SH DESCRIPTION
20 .TP
21 .BR strcpy ()
22 This function copies the string pointed to by
23 .IR src ,
24 into a string
25 at the buffer pointed to by
26 .IR dst .
27 The programmer is responsible for allocating a destination buffer large enough,
28 that is,
29 .IR "strlen(src) + 1" .
30 .IP
31 It is equivalent to
32 .IP
33 .in +4n
34 .EX
35 stpcpy(dst, src), dst
36 .EE
37 .in
38 .TP
39 .BR strcat ()
40 This function catenates the string pointed to by
41 .IR src ,
42 after the string pointed to by
43 .I dst
44 (overwriting its terminating null byte).
45 The programmer is responsible for allocating a destination buffer large enough,
46 that is,
47 .IR "strlen(dst) + strlen(src) + 1" .
48 .IP
49 It is equivalent to
50 .IP
51 .in +4n
52 .EX
53 stpcpy(strnul(dst), src), dst
54 .EE
55 .in
56 .SH RETURN VALUE
57 These functions return
58 .IR dst .
59 .SH ATTRIBUTES
60 For an explanation of the terms used in this section, see
61 .BR attributes (7).
62 .TS
63 allbox;
64 lbx lb lb
65 l l l.
66 Interface Attribute Value
67 T{
68 .na
69 .nh
70 .BR strcpy (),
71 .BR strcat ()
72 T} Thread safety MT-Safe
73 .TE
74 .SH STANDARDS
75 C11, POSIX.1-2008.
76 .SH HISTORY
77 POSIX.1-2001, C89, SVr4, 4.3BSD.
78 .SH CAVEATS
79 The strings
80 .I src
81 and
82 .I dst
83 may not overlap.
84 .P
85 If the destination buffer is not large enough,
86 the behavior is undefined.
87 See
88 .B _FORTIFY_SOURCE
89 in
90 .BR feature_test_macros (7).
91 .P
92 .BR strcat ()
93 can be very inefficient.
94 Read about
95 .UR https:\://www.joelonsoftware.com/\:2001/12/11/\:back\-to\-basics/
96 Shlemiel the painter
97 .UE .
98 .SH EXAMPLES
99 .\" SRC BEGIN (strcpy.c)
100 .EX
101 #include <err.h>
102 #include <stdio.h>
103 #include <stdlib.h>
104 #include <string.h>
105 \&
106 int
107 main(void)
108 {
109 char *buf1;
110 size_t len, size;
111 \&
112 size = strlen("Hello ") + strlen("world") + strlen("!") + 1;
113 buf1 = malloc(sizeof(*buf1) * size);
114 if (buf1 == NULL)
115 err(EXIT_FAILURE, "malloc()");
116 \&
117 strcpy(buf1, "Hello ");
118 strcat(buf1, "world");
119 strcat(buf1, "!");
120 len = strlen(buf1);
121 \&
122 printf("[len = %zu]: ", len);
123 puts(buf1); // "Hello world!"
124 free(buf1);
125 \&
126 exit(EXIT_SUCCESS);
127 }
128 .EE
129 .\" SRC END
130 .SH SEE ALSO
131 .BR stpcpy (3),
132 .BR strdup (3),
133 .BR string (3),
134 .BR wcscpy (3),
135 .BR string_copying (7)