当前位置: C语言 -- 标准库 -- <string.h> -- strncat

strncat函数


概要:
#include <string.h>
char *strncat(char * restrict s1, 
      const char * restrict s2, 
      size_t n);

描述:

该函数从参数s2指向的数组中添加不超过n个字符(空字符和空字符后的字符不会被添加。)到参数s1指向字符串的末尾。参数s2指向数组的初始字符覆盖参数s1指向字符串的终止空字符。结果会被添加终止空字符,因此添加后参数s1指向数组的最大字符数是strlen(s1)+n+1

参数s1指向的数组应足够大,能够容纳生成的结果字符串。

如果复制发生在重叠对象之间,函数行为是未定义的。

ISO/IEC 9899:2018标准定义了该函数的安全版本strncat_s


参数:
char * restrict s1

指向目标数组的指针,结果字符串将存入该数组。

const char * restrict s2

指向源数组的指针,从该数组中读取字符。

size_t n

最多可以添加的字符数。


返回值:

函数返回s1的值。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
/*函数strncat范例*/

#include <stdio.h>
#include <string.h>

int main(void)
{
    char strOne[12] = "Hello ";
    const char strTwo[] = "Tom and Jack";

    strncat(strOne, strTwo, 3);
    puts(strOne);
    
    return 0;
}


输出:

Hello Tom


相关内容:
strcat 连接字符串的函数。