#include <string.h>
char *strncpy( char *to, const char *from, size_t count );
将字符串 from 中至多 count 个字符复制到字符串 to 中。如果字符串 from 的长度小于 count,其余部分用 '\0' 填补。返回处理完成的字符串。
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
strncpy(string, str1, 3);
string[3] = '\0';
printf("%s\n", string);
return 0;
}输出结果:
abc