NAME
strncat - append non-null bytes from a source array to a string, and null-terminate the result
LIBRARY
Standard C library (libc, -lc)
SYNOPSIS
#include <string.h>char *strncat(char *restrict \ndst\n, const char \nsrc\n[restrict .\nssize\n],\n
\n size_t \nssize\n);DESCRIPTION
This function appends at most ssize non-null bytes from the array pointed to by src, followed by a null character, to the end of the string pointed to by dst. dst must point to a string contained in a buffer that is large enough, that is, the buffer size must be at least strlen(dst) + strnlen(src, ssize) + 1.
An implementation of this function might be:
char *
strncat(char *restrict dst, const char *restrict src, size_t ssize)
{
\n
#define strnul(s) (s + strlen(s))
\n
stpcpy(mempcpy(strnul(dst), src, strnlen(src, ssize)), "");
\n
return dst;
}RETURN VALUE
strncat() returns dst.
ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7).
| Interface | Attribute | Value |
| strncat () | Thread safety | MT-Safe |
STANDARDS
C11, POSIX.1-2008.
HISTORY
POSIX.1-2001, C89, SVr4, 4.3BSD.
CAVEATS
The name of this function is confusing; it has no relation to strncpy(3).
If the destination buffer does not already contain a string, or is not large enough, the behavior is undefined. See _FORTIFY_SOURCE in feature_test_macros(7).
BUGS
This function can be very inefficient. Read about Shlemiel the painter (opens in new tab).
EXAMPLES
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define nitems(arr) (sizeof((arr)) / sizeof((arr)[0]))
int
main(void)
{
\n
size_t n;
\n
// Null-padded fixed-size character sequences
\n
char pre[4] = "pre.";
\n
char new_post[50] = ".foo.bar";
\n
// Strings
\n
char post[] = ".post";
\n
char src[] = "some_long_body.post";
\n
char *dest;
\n
n = nitems(pre) + strlen(src) - strlen(post) + nitems(new_post) + 1;
\n
dest = malloc(sizeof(*dest) * n);
\n
if (dest == NULL)
\n
err(EXIT_FAILURE, "malloc()");
\n
dest[0] = '\0'; // There's no 'cpy' function to this 'cat'.
\n
strncat(dest, pre, nitems(pre));
\n
strncat(dest, src, strlen(src) - strlen(post));
\n
strncat(dest, new_post, nitems(new_post));
\n
puts(dest); // "pre.some_long_body.foo.bar"
\n
free(dest);
\n
exit(EXIT_SUCCESS);
}SEE ALSO
string(3), string_copying(7)