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.


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


This function has the following parameter.

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

size_t is an unsigned integral type.


destination is returned.


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

The code above generates the following result.