【C语法学习】27 - strncat()函数

1 函数原型

strncat():将src指向的字符串的前n个字符附加在dest指向的字符串末尾,函数原型如下:

char *strncat(char *dest, const char *src, size_t n);

2 参数

strncat()函数有三个参数src、dest和n:

  1. 参数src是指向源字符串的指针,类型为char*型;
  2. 参数dest是指向目的字符串的指针,类型为char*型;
  3. 参数n是附加的字符数量,类型为size_t(unsigned int)型。

3 返回值

strncat()函数的返回值类型为char*型,返回值为dest。

4 使用说明

  1. 当src指向的字符串长度小于n,strncat()函数的功能与strcat()函数的功能相同;
  2. 当src指向的字符串长度大于n,将前n个字符附加在dest指向的字符串末尾,然后在拼接后的字符串末尾添加空字符’\0’。

C语言标准描述如下:

1. Appends at most count characters from the character array pointed to by src, stopping if the null character is found, to the end of the null-terminated byte string pointed to by dest. 
2. The character src[0] replaces the null terminator at the end of dest. 
3. The terminating null character is always appended in the end (so the maximum number of bytes the function may write is count+1).
4. The behavior is undefined if the destination array does not have enough space for the contents of both dest and the first count characters of src, plus the terminating null character. 
5. The behavior is undefined if the source and destination objects overlap. 
6. The behavior is undefined if either dest is not a pointer to a null-terminated byte string or src is not a pointer to a character array,

5 示例

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值