strcat() 函数定义在 string.h 头文件中。它通过将 src 指向的字符串附加到 dest 指向的目标字符串来连接两个字符串。
char *strcat(char *dest, const char *src); #where dest and src should be strings
strcat() 函数接受两个参数。如果目标字符串的大小不足以存储结果字符串,则会引发段错误。
| 参数 | 描述 | 必需/可选 |
|---|---|---|
| dest | 指向目标数组的指针包含连接后的结果字符串 | 必需 |
| src | 这是要附加的字符串 | 必需 |
该函数返回一个指向结果目标字符串的指针。
| 输入 | 返回值 |
|---|---|
| 成功 | 指向目标字符串的指针 |
#include <stdio.h>
#include <string.h>
int main()
{
char src[40], dest[40];
strcpy(src, "Source is:");
strcpy(dest, "Destination is:");
strcat(dest, src);
printf("Final destination string : |%s|", dest);
return(0);
}
输出
Final destination string : |Destination is Source is|
#include <stdio.h>
#include <string.h>
int main (){
char string1[100] = "This is ", string2[] = "learnetutorials.com";
// the resultant string is stored in string1.
strcat(string1, string2);
puts(string1);
puts(string2);
return 0;
}
输出
This is learnetutorials.com learnetutorials.com