1STRCAT(3) Linux Programmer's Manual STRCAT(3)
2
3
4
6 strcat, strncat - concatenate two strings
7
9 #include <string.h>
10
11 char *strcat(char *restrict dest, const char *restrict src);
12 char *strncat(char *restrict dest, const char *restrict src, size_t n);
13
15 The strcat() function appends the src string to the dest string, over‐
16 writing the terminating null byte ('\0') at the end of dest, and then
17 adds a terminating null byte. The strings may not overlap, and the
18 dest string must have enough space for the result. If dest is not
19 large enough, program behavior is unpredictable; buffer overruns are a
20 favorite avenue for attacking secure programs.
21
22 The strncat() function is similar, except that
23
24 * it will use at most n bytes from src; and
25
26 * src does not need to be null-terminated if it contains n or more
27 bytes.
28
29 As with strcat(), the resulting string in dest is always null-termi‐
30 nated.
31
32 If src contains n or more bytes, strncat() writes n+1 bytes to dest (n
33 from src plus the terminating null byte). Therefore, the size of dest
34 must be at least strlen(dest)+n+1.
35
36 A simple implementation of strncat() might be:
37
38 char *
39 strncat(char *dest, const char *src, size_t n)
40 {
41 size_t dest_len = strlen(dest);
42 size_t i;
43
44 for (i = 0 ; i < n && src[i] != '\0' ; i++)
45 dest[dest_len + i] = src[i];
46 dest[dest_len + i] = '\0';
47
48 return dest;
49 }
50
52 The strcat() and strncat() functions return a pointer to the resulting
53 string dest.
54
56 For an explanation of the terms used in this section, see at‐
57 tributes(7).
58
59 ┌────────────────────────────────────────────┬───────────────┬─────────┐
60 │Interface │ Attribute │ Value │
61 ├────────────────────────────────────────────┼───────────────┼─────────┤
62 │strcat(), strncat() │ Thread safety │ MT-Safe │
63 └────────────────────────────────────────────┴───────────────┴─────────┘
64
66 POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
67
69 Some systems (the BSDs, Solaris, and others) provide the following
70 function:
71
72 size_t strlcat(char *dest, const char *src, size_t size);
73
74 This function appends the null-terminated string src to the string
75 dest, copying at most size-strlen(dest)-1 from src, and adds a termi‐
76 nating null byte to the result, unless size is less than strlen(dest).
77 This function fixes the buffer overrun problem of strcat(), but the
78 caller must still handle the possibility of data loss if size is too
79 small. The function returns the length of the string strlcat() tried
80 to create; if the return value is greater than or equal to size, data
81 loss occurred. If data loss matters, the caller must either check the
82 arguments before the call, or test the function return value. strl‐
83 cat() is not present in glibc and is not standardized by POSIX, but is
84 available on Linux via the libbsd library.
85
87 Because strcat() and strncat() must find the null byte that terminates
88 the string dest using a search that starts at the beginning of the
89 string, the execution time of these functions scales according to the
90 length of the string dest. This can be demonstrated by running the
91 program below. (If the goal is to concatenate many strings to one tar‐
92 get, then manually copying the bytes from each source string while
93 maintaining a pointer to the end of the target string will provide bet‐
94 ter performance.)
95
96 Program source
97
98 #include <stdint.h>
99 #include <string.h>
100 #include <time.h>
101 #include <stdio.h>
102
103 int
104 main(int argc, char *argv[])
105 {
106 #define LIM 4000000
107 char p[LIM + 1]; /* +1 for terminating null byte */
108 time_t base;
109
110 base = time(NULL);
111 p[0] = '\0';
112
113 for (int j = 0; j < LIM; j++) {
114 if ((j % 10000) == 0)
115 printf("%d %jd\n", j, (intmax_t) (time(NULL) - base));
116 strcat(p, "a");
117 }
118 }
119
121 bcopy(3), memccpy(3), memcpy(3), strcpy(3), string(3), strncpy(3), wc‐
122 scat(3), wcsncat(3)
123
125 This page is part of release 5.12 of the Linux man-pages project. A
126 description of the project, information about reporting bugs, and the
127 latest version of this page, can be found at
128 https://www.kernel.org/doc/man-pages/.
129
130
131
132GNU 2021-03-22 STRCAT(3)