Saturday, February 4, 2017

String N Copy Example

Following example will copy N characters from str2 to str1.

#include <stdio.h> 
#include <string.h> 
int main () 

char str1[10] = "12345"; 
char str2[10] = "World"; 

strncpy(str1, str2, 4); 
str1[4]='\0';

printf("strncpy( str1, str2, 3) = %s\n", str1 ); 

return 0; 
}

Output of the program

strncpy( str1, str2, 3) = Worl


* * * * *

No comments:

Post a Comment