<string.h> 函数

Cstrcat()

strcat() 函数的定义是:

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

它定义在 string.h 头文件中。


strcat() 参数

如你所见,strcat() 函数接受两个参数:

destination - 目标字符串
source - 源字符串


strcat() 函数将 destination 字符串和 source 字符串连接起来,结果存储在 destination 字符串中。


示例:C strcat() 函数

#include <stdio.h>
#include <string.h>
int main() {
   char str1[100] = "This is ", str2[] = "programiz.com";

   // concatenates str1 and str2
   // the resultant string is stored in str1.
   strcat(str1, str2);

   puts(str1);
   puts(str2);

   return 0;
}

输出

This is programiz.com
programiz.com

注意:当我们使用 strcat() 时,目标字符串的大小应足以存储结果字符串。否则,我们会收到段错误(segmentation fault)。

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战