1、函数原型。
#include <stdio.h> char *strncpy(char *s1, const char *s2, size_t n) { char *tmp = s1; while(n) { if(!(*s1++ = *s2++)) break; n--; } while(n--) *s1++ = '\0'; return tmp; } int main(void) { char str[128]; char test[128] = "abcdefg"; printf("result: %s\n", strncpy(str, test, 4)); return 0; }
2、
#include <stdio.h> #include <string.h> int main(void) { char str[128]; char tmp[128] = "abcdefg"; printf("result: %s\n", strncpy(str, tmp, 5)); return 0; }