c语言中strncpy函数
作者:互联网
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; }
标签:tmp,函数,s1,char,str,128,strncpy,语言 来源: https://www.cnblogs.com/liujiaxin2018/p/14833492.html