C Standard Library

C <string.h> - strncat() Function



The C <string.h> strncat() function is used to append first num characters of byte string pointed to by source to the end of byte string pointed to by destination. The terminating null character in destination is overwritten by the first character of source, and the resulting byte string is null-terminated.

The behavior is undefined if the destination is not large enough for the content of resulting byte string (including the null character), or if the strings overlap.

Syntax

char * strncat ( char * destination, const char * source, size_t num );

Parameters

destination Specify pointer to the null-terminated byte string to append to.
source Specify pointer to the null-terminated byte string to copy from.
num Specify maximum number of characters to copy.
size_t is an unsigned integer type.

Return Value

Returns destination.

Example:

The example below shows the usage of strncat() function.

#include <stdio.h>
#include <string.h>
 
int main (){
  char str1[100] = "Hello ";
  char str2[100] = "World!. Programming is fun.";
  
  //concatenating str1 with first 
  //7 characters of str2
  strncat(str1, str2, 7);
  printf("str1 is: %s\n", str1);  
 
  return 0;
}

The output of the above code will be:

str1 is: Hello World!.

❮ C <string.h> Library