Mega Code Archive

 
Categories / C Tutorial / String h
 

Strncpy

Item Value Header filestring.h Declarationchar *strncpy(char *str1, const char *str2, size_t count); Functioncopies up to count characters from *str2 to *str1. Returnreturns a pointer to str1. str2 must be a pointer to a null-terminated string. If *str2 has less than 'count' characters, nulls will be appended to *str1 until count characters have been copied. If *str2 is longer than count characters, the result *str1 will not be null terminated. #include<stdio.h> #include<string.h> int main(void){   char str1[128], str2[80];   gets(str1);   strncpy(str2, str1, 79);   printf("%s",str2); } 2 2