C String Functions - C strncat






Appends the string str2 to the end of the string str1 up to n characters long.

The terminating null character of str1 is overwritten.

Copying stops once n characters are copied or the terminating null character of str2 is copied.

A terminating null character is always appended to str1. If overlapping occurs, the result is undefined.

The argument str1 is returned.

Prototype

char *strncat(char *str1 , const char *str2 , size_t n );

Parameter

This function has the following parameter.

destination
Pointer to the destination array.
source
C string to be appended.
num
Maximum number of characters to be appended.

size_t is an unsigned integral type.

Return

destination is returned.

Example


#include <stdio.h>
#include <string.h>
/*from  w  w  w.  j  a v a 2  s  .c  o m*/
int main (){
  char str1[20];
  char str2[20];
  strcpy (str1,"To be ");
  strcpy (str2,"or not to be");
  strncat (str1, str2, 6);
  puts (str1);
  return 0;
}        

The code above generates the following result.





Example 2


#include <string.h> 
#include <stdio.h>
#include <stdlib.h>
 //from  w  ww.  ja v  a2  s. c o m
int main(void) 
{
    char str[50] = "Hello ";
    char str2[50] = "World!";
    strcat(str, str2);
    strncat(str, " Goodbye World!", 3);
    puts(str);
 
}

The code above generates the following result.