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 *dest, const char *src);
12
13 char *strncat(char *dest, const char *src, size_t n);
14
16 The strcat() function appends the src string to the dest string, over‐
17 writing the terminating null byte ('\0') at the end of dest, and then
18 adds a terminating null byte. The strings may not overlap, and the
19 dest string must have enough space for the result. If dest is not
20 large enough, program behavior is unpredictable; buffer overruns are a
21 favorite avenue for attacking secure programs.
22
23 The strncat() function is similar, except that
24
25 * it will use at most n bytes from src; and
26
27 * src does not need to be null-terminated if it contains n or more
28 bytes.
29
30 As with strcat(), the resulting string in dest is always null-termi‐
31 nated.
32
33 If src contains n or more bytes, strncat() writes n+1 bytes to dest (n
34 from src plus the terminating null byte). Therefore, the size of dest
35 must be at least strlen(dest)+n+1.
36
37 A simple implementation of strncat() might be:
38
39 char*
40 strncat(char *dest, const char *src, size_t n)
41 {
42 size_t dest_len = strlen(dest);
43 size_t i;
44
45 for (i = 0 ; i < n && src[i] != '\0' ; i++)
46 dest[dest_len + i] = src[i];
47 dest[dest_len + i] = '\0';
48
49 return dest;
50 }
51
53 The strcat() and strncat() functions return a pointer to the resulting
54 string dest.
55
57 SVr4, 4.3BSD, C89, C99.
58
60 Some systems (the BSDs, Solaris, and others) provide the following
61 function:
62
63 size_t strlcat(char *dest, const char *src, size_t size);
64
65 This function appends the null-terminated string src to the string
66 dest, copying at most size-strlen(dest)-1 from src, and adds a null
67 terminator to the result, unless size is less than strlen(dest). This
68 function fixes the buffer overrun problem of strcat(), but the caller
69 must still handle the possibility of data loss if size is too small.
70 The function returns the length of the string strlcat() tried to cre‐
71 ate; if the return value is greater than or equal to size, data loss
72 occurred. If data loss matters, the caller must either check the argu‐
73 ments before the call, or test the function return value. strlcat() is
74 not present in glibc and is not standardized by POSIX, but is available
75 on Linux via the libbsd library.
76
78 bcopy(3), memccpy(3), memcpy(3), strcpy(3), string(3), strncpy(3),
79 wcscat(3), wcsncat(3)
80
82 This page is part of release 3.53 of the Linux man-pages project. A
83 description of the project, and information about reporting bugs, can
84 be found at http://www.kernel.org/doc/man-pages/.
85
86
87
88GNU 2012-07-19 STRCAT(3)