1STRCAT(3)                  Linux Programmer's Manual                 STRCAT(3)
2
3
4

NAME

6       strcat, strncat - concatenate two strings
7

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

53       The strcat() and strncat() functions return a pointer to the  resulting
54       string dest.
55

ATTRIBUTES

57       For   an   explanation   of   the  terms  used  in  this  section,  see
58       attributes(7).
59
60       ┌────────────────────┬───────────────┬─────────┐
61Interface           Attribute     Value   
62       ├────────────────────┼───────────────┼─────────┤
63strcat(), strncat() │ Thread safety │ MT-Safe │
64       └────────────────────┴───────────────┴─────────┘

CONFORMING TO

66       POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
67

NOTES

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

EXAMPLE

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 <string.h>
99       #include <time.h>
100       #include <stdio.h>
101
102       int
103       main(int argc, char *argv[])
104       {
105       #define LIM 4000000
106           int j;
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 (j = 0; j < LIM; j++) {
114               if ((j % 10000) == 0)
115                   printf("%d %ld\n", j, (long) (time(NULL) - base));
116               strcat(p, "a");
117           }
118       }
119

SEE ALSO

121       bcopy(3),  memccpy(3),  memcpy(3),  strcpy(3),  string(3),  strncpy(3),
122       wcscat(3), wcsncat(3)
123

COLOPHON

125       This  page  is  part of release 5.04 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                               2019-08-02                         STRCAT(3)
Impressum