Chapter Contents

Previous

Next
strncat

strncat



Concatenate Two Null-Terminated Strings (Limited)

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <string.h>

char *strncat(char *to, const char *from, size_t maxlen);


DESCRIPTION

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.


RETURN VALUE

A pointer to the to string is returned.


CAUTION

A protection or addressing exception may occur if the 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 .


EXAMPLE

#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);
}


RELATED FUNCTIONS

strcat , strncpy


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.