strcat 和strncat 函数学习

本文详细介绍了C语言中用于字符串连接的两个重要函数:strcat() 和 strncat()。strcat() 用于将一个字符串完整地连接到另一个字符串后面,并在连接后的字符串末尾添加 null 字符。而 strncat() 则允许开发者指定要连接的字符数量,更加灵活可控。

函数原型

char * strcat ( char * destination, const char * source );

Concatenate strings连接字符串

Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a new null-character is appended at the end of the new string formed by the concatenation of both in destination.

将source的字符串连接到destination上,最后的null被覆盖,然后再最后加个null字符。

Parameters

destination
Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.
source
C string to be appended. This should not overlap destination.
/* strcat example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;
}

输出结果
these strings are concatenated.

strncat 指定长度的链接字符串。

Append characters from string

Appends the first num characters of source to destination, plus a terminating null-character. If the length of the C string in source is less than num, only the content up to the terminating null-character is copied.

Parameters

destination
Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string, including the additional null-character.
source
C string to be appended.
num
Maximum number of characters to be appended.


Return Value

destination is returned.

/* strncat example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[20];
  char str2[20];
  strcpy (str1,"To be ");
  strcpy (str2,"or not to be");
  strncat (str1, str2, 6);
  puts (str1);
  return 0;
}


Output:


To be or not

`strcat` `strncat` 都是 C 语言中的字符串拼接函数,用于将一个字符串(源字符串)追加到另一个字符串(目标字符串)的末尾。它们的区别主要在于安全性: ### 1. `strcat`(不检查缓冲区溢出) ```c char *strcat(char *dest, const char *src); ``` - **功能**:将 `src` 指向的字符串追加到 `dest` 指向的字符串末尾(覆盖 `dest` 的 `\0`)。 - **问题**:如果 `dest` 的空间不足以容纳追加后的字符串,会导致**缓冲区溢出**(Undefined Behavior)。 - **示例**: ```c char dest[10] = "hello"; strcat(dest, "world"); // 可能溢出,因为 dest 只有 10 字节 ``` ### 2. `strncat`(安全版本,限制追加长度) ```c char *strncat(char *dest, const char *src, size_t n); ``` - **功能**:从 `src` 追加**最多 `n` 个字符**到 `dest` 末尾(自动补 `\0`)。 - **优点**:避免溢出,但需确保 `dest` 剩余空间 ≥ `n + 1`(含 `\0`)。 - **示例**: ```c char dest[10] = "hello"; strncat(dest, "world", 4); // 追加 "worl",结果 "helloworl\0" ``` ### 关键区别 | 特性 | `strcat` | `strncat` | |---------------|------------------------|-------------------------------| | 安全性 | 不安全(可能溢出) | 安全(可限制长度) | | 参数 | `dest, src` | `dest, src, n`(最大追加长度)| | 自动补 `\0` | 是 | 是(总追加 `n+1` 字节) | ### 最佳实践 - 优先使用 `strncat`,并确保 `n` ≤ `dest` 剩余空间 - 1(留位置给 `\0`)。 - 检查目标缓冲区大小是否足够,例如: ```c char dest[20] = "hello"; size_t remaining_size = sizeof(dest) - strlen(dest) - 1; strncat(dest, "world", remaining_size); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值