#include <string.h> char *strcat(char *dest, const char *src); char *strncat(char *dest, const char *src, size_t n);
The strncat() function is similar, except that
As with strcat(), the resulting string in dest is always null-terminated.
If src contains n or more bytes, strncat() writes n+1 bytes to dest (n from src plus the terminating null byte). Therefore, the size of dest must be at least strlen(dest)+n+1.
A simple implementation of strncat() might be:
char *
strncat(char *dest, const char *src, size_t n)
{
size_t dest_len = strlen(dest);
size_t i;
for (i = 0 ; i < n && src[i] != '\0' ; i++)
dest[dest_len + i] = src[i];
dest[dest_len + i] = '\0';
Interface | Attribute | Value |
strcat(), strncat() | Thread safety | MT-Safe |
size_t strlcat(char *dest, const char *src, size_t size);
This function appends the null-terminated string src to the string dest, copying at most size-strlen(dest)-1 from src, and adds a terminating null byte to the result, unless size is less than strlen(dest). This function fixes the buffer overrun problem of strcat(), but the caller must still handle the possibility of data loss if size is too small. The function returns the length of the string strlcat() tried to create; if the return value is greater than or equal to size, data loss occurred. If data loss matters, the caller must either check the arguments before the call, or test the function return value. strlcat() is not present in glibc and is not standardized by POSIX, but is available on Linux via the libbsd library.
int
main(int argc, char *argv[])
{
#define LIM 4000000
char p[LIM + 1]; /* +1 for terminating null byte */
time_t base;
base = time(NULL);
p[0] = '\0';
for (int j = 0; j < LIM; j++) {
if ((j % 10000) == 0)
printf("%d %jd\n", j, (intmax_t) (time(NULL) - base));
strcat(p, "a");
}
}