strncpy() copy n chars in C.

 

01  #include <stdio.h>
02 #include <string.h>
03
04 int main()
05 {
06 char src_str[30] = "Hello students";
07 char dest_str[30];
08
09 printf("src_str: %sn", src_str);
10 strncpy(dest_str, src_str,5);
11 printf("dest_str: %sn", dest_str);
12
13 strncpy(dest_str, "Copy first 13 charaters",13);
14 printf("dest_str: %sn", dest_str);
15
16 return 0;
17 }
 
 
OUTPUT :

src_str: Hello students
dest_str: Hello
dest_str: Copy first 13

/* strncpy copies up to specified characters from src into dest, truncating or
null-padding dest. The target string, dest, might not be null-terminated if the length of src
is maxlen or more. */

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *