C 标准库 - <string.h>

C 语言 strcat() 函数用于将两个字符串拼接起来:把 source 追加到 destination 的末尾,并在结果后补上空字符。

原型

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

参数

  • destination - 目标字符串,必须足够大以容纳拼接后的结果;
  • source - 要追加的源字符串。

返回值

返回 destination 的指针。

示例

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

int main()
{
    char str1[50] = "Hello ";
    char str2[] = "World!";

    strcat(str1, str2);
    printf("拼接结果: %s\n", str1);

    return 0;
}

输出:

拼接结果: Hello World!

注意事项

destination 的空间必须足够容纳"原内容 + source + \0",否则会越界写入。