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)。