C String Function : strncpy()

The strncpy() function is used to copy the one string's content to another string. It is same as strcpy() function except that in strncpy() we have to specify how much byte of source data in copied into the destination. The syntax of strcpy() is :
char * strncat ( char * destination, const char * source, size_t num );
Where :
  • destination is the string where you want to copy.
  • source is the trying which you want to copy on the destination.
  • num specify the bytes you want to copy into destination.
Example :
strncpy(str1, str2, 5);
The above statement copies the first 5 byte of  str2's content to str1. In strncat( ) operation, null character of destination string is overwritten by source string’s first character and null character is added at the end of new destination string which is created after strncat( ) operation.

Example C Program :
#include <stdio.h>
#include <string.h>

int main() {
  char str1[30];
  char str2[] = "Hello world, This is String.";

  strncpy(str1, str2, 5);
  printf("%s\n", str1);
  return 0;
Output :


Note : We have to include the string.h header in our program to use the string function.

Links :