The strncpy() function is similar to the strcpy() function, except that it copies only the specified number of characters from source string to destination string.
C strncpy() declaration
char *strncpy(char *str1, const char *str2, size_t n)
str1 – Destination string. The string in which the first n characters of source string str2 are copied.
str2 – Source string
n – number of characters of source string that needs to be copied.
Return value of strncpy()
It returns the pointer to the destination string after copying the n characters of source string into it.
Example: strncpy() function
#include <stdio.h>
#include <string.h>
int main () {
   char str1[20]; 
   char str2[25];
   /* The first argument in the function is destination string. 
    * In this case we are doing full copy using the strcpy() function. 
    * Here string str2 is destination string in which we are copying the 
    * specified string 
    */ 
   strcpy(str2, "welcome to beginnersbook.com");
   /* In this case we are doing a limited copy using the strncpy()
    * function. We are copying only the 7 characters of string str2 to str1
    */
   strncpy(str1, str2, 7);
   printf("String str1: %s\n", str1); 
   printf("String str2: %s\n", str2);
   return 0;
}
Output:
String str1: welcome String str2: welcome to beginnersbook.com
Leave a Reply