#include <string.h> char *strncat(char *to, const char *from, size_t maxlen);
strncat
copies characters from the second argument string (from
)
to the end of the first argument string (to
) until a
terminating-null character is found or until the number of characters specified by
maxlen
have been copied. After the maximum number of characters is
reached, a terminating-null character is added to the output string.
to
string is returned.
to
string is
not properly terminated.
The effect of strncat
is not defined if the to
and from
areas overlap.
If the maxlen
value is 0, no characters are copied. If the value
is negative, it is interpreted as a very large unsigned number, causing
the number of characters copied to be essentially unlimited.
Because a null terminator is always appended to the to
string,
maxlen+1
characters are copied if the length of the from
string is
greater than maxlen
.
#include <string.h> #include <stdio.h> #define MAXLINE 100 #define PRINTAMT 20 main() { char line[MAXLINE]; char intro[] = "Your input was:"; char outline[sizeof(intro)+PRINTAMT]; /* space for output message */ puts("Enter a line of input:"); gets(line); strcpy(outline, intro); strncat(outline, line, PRINTAMT); /* Append input to output. */ puts(outline); printf("Your input was truncated if it was longer" " than %d characters.", PRINTAMT); }
strcat
, strncpy
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.