1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
29 .\" Modified Sat Jul 24 18:11:47 1993 by Rik Faith (faith@cs.unc.edu)
30 .\" 2007-06-15, Marc Boyer <marc.boyer@enseeiht.fr> + mtk
31 .\" Improve discussion of strncat().
32 .TH STRCAT 3 2020-11-01 "GNU" "Linux Programmer's Manual"
34 strcat, strncat \- concatenate two strings
37 .B #include <string.h>
39 .BI "char *strcat(char *" dest ", const char *" src );
40 .BI "char *strncat(char *" dest ", const char *" src ", size_t " n );
50 overwriting the terminating null byte (\(aq\e0\(aq) at the end of
52 and then adds a terminating null byte.
53 The strings may not overlap, and the
56 enough space for the result.
59 is not large enough, program behavior is unpredictable;
60 .IR "buffer overruns are a favorite avenue for attacking secure programs" .
64 function is similar, except that
73 does not need to be null-terminated if it contains
79 the resulting string in
81 is always null-terminated.
96 plus the terminating null byte).
97 Therefore, the size of
100 .IR "strlen(dest)+n+1" .
102 A simple implementation of
109 strncat(char *dest, const char *src, size_t n)
111 size_t dest_len = strlen(dest);
114 for (i = 0 ; i < n && src[i] != \(aq\e0\(aq ; i++)
115 dest[dest_len + i] = src[i];
116 dest[dest_len + i] = \(aq\e0\(aq;
127 functions return a pointer to the resulting string
130 For an explanation of the terms used in this section, see
138 Interface Attribute Value
142 T} Thread safety MT-Safe
148 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
150 Some systems (the BSDs, Solaris, and others) provide the following function:
152 size_t strlcat(char *dest, const char *src, size_t size);
154 This function appends the null-terminated string
159 .IR "size\-strlen(dest)\-1"
162 and adds a terminating null byte to the result,
167 This function fixes the buffer overrun problem of
169 but the caller must still handle the possibility of data loss if
172 The function returns the length of the string
174 tried to create; if the return value is greater than or equal to
177 If data loss matters, the caller
179 either check the arguments before the call, or test the function return value.
181 is not present in glibc and is not standardized by POSIX,
182 .\" https://lwn.net/Articles/506530/
183 but is available on Linux via the
192 must find the null byte that terminates the string
194 using a search that starts at the beginning of the string,
195 the execution time of these functions
196 scales according to the length of the string
198 This can be demonstrated by running the program below.
199 (If the goal is to concatenate many strings to one target,
200 then manually copying the bytes from each source string
201 while maintaining a pointer to the end of the target string
202 will provide better performance.)
213 main(int argc, char *argv[])
216 char p[LIM + 1]; /* +1 for terminating null byte */
222 for (int j = 0; j < LIM; j++) {
223 if ((j % 10000) == 0)
224 printf("%d %jd\en", j, (intmax_t) (time(NULL) \- base));