strncpy(): Copying part of strings in C
#include <stdio.h>
#include <string.h>
int main(void){
char var1[20]="First one";
char var2[]="Second two";
strncpy(var2,var1,5);
printf(var2);
return 0;
}
The output of above code is here, ( first 5 chars of var1 variable is copied to var2 variable )
Firstd two
We can use string function strncpy()
to copy part strings.
Part of the second string is added to the first string.
strcpy(destination_string, source_string, n);
- Source string is not affected
- Destination string is replaced by source string.
This article is written by plus2net.com team.
plus2net.com