This documentation is copyright © 1998-2001 Sandro Sigala <sandro@sigala.it>. All rights reserved. Released under the GNU General Public License.
![]() |
![]() |
![]() |
![]() |
#include <string.h> char *strncat(char *dest, const char *src, size_t n);
n
characters of the src
string to
the dest
string overwriting
the terminating null character at the end of dest
, and then
adds a terminating null character. The strings should not overlap, and
the dest
buffer size should be greater or equal to
strlen(dest) + n + 1
. Returns the dest
parameter.
n
parameter less than the src
string length:
char buf[13]; strcpy(buf, "ray"); strncat(buf, "-tracing", 4);this is the state of the
buf
array before and after the
call of strncat
(? means
any character):
In this second example we catenate two strings, specifing a n
parameter
greater that the src
string length:
char buf[13]; strcpy(buf, "ray"); strncat(buf, "-tracing", 10);this is the state of the
buf
array before and after the
call of strncat
:
#include <string.h> char *strncat(char *dest, const char *src, size_t n) { char *dp; for (dp = dest; *dp != '\0'; ++dp) ; while (n-- > 0 && (*dp++ = *src++) != '\0') ; *dp = '\0'; return dest; }
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char buf[7]; strcpy(buf, "abc"); strncat(buf, "defghi", 3); printf("%s\n", buf); /* Prints "abcdef". */ printf("%d\n", strlen(buf)); /* Prints "6". */ return EXIT_SUCCESS; }