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.

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer