c语言中strncat函数、函数原型、头文件
1、函数原型
#include <stdio.h> char *strncat(char *s1, const char *s2, size_t n) { char *tmp = s1; while(*s1) s1++; while(n--) { if(!(*s1++ = *s2++)) break; } *s1 = '\0'; return tmp; } int main(void) { char str1[128] = "abcd"; char str2[128]; printf("str2: "); scanf("%s", str2); unsigned n; printf("n = "); scanf("%u", &n); strncat(str1, str2, n); printf("concatenate result: %s\n", str1); return 0; }
2、头文件
#include <stdio.h> #include <string.h> int main(void) { char str1[128] = "abcd"; char str2[128]; printf("str2: "); scanf("%s", str2); unsigned n; printf("n = "); scanf("%u", &n); strncat(str1, str2, n); printf("concatenate result: %s\n", str1); return 0; }