ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# C `strcat()`函数 > 原文: [https://beginnersbook.com/2017/11/c-strcat-function-with-example/](https://beginnersbook.com/2017/11/c-strcat-function-with-example/) `strcat()`函数用于字符串连接。它在另一个指定字符串的末尾连接指定的字符串。在本教程中,我们将看到带示例的 `strcat()`函数。 ## C `strcat()`声明 ```c char *strcat(char *str1, const char *str2) ``` 此函数将两个指针作为参数,并在连接后返回指向目标字符串的指针。 `str1` - 指向目标字符串的指针。 `str2` - 指向附加到目标字符串的源字符串的指针。 ## 示例:C `strcat()`函数 ```c #include <stdio.h> #include <string.h> int main () { char str1[50], str2[50]; //destination string strcpy(str1, "This is my initial string"); //source string strcpy(str2, ", add this"); //concatenating the string str2 to the string str1 strcat(str1, str2); //displaying destination string printf("String after concatenation: %s", str1); return(0); } ``` 输出: ```c String after concatenation: This is my initial string, add this ``` 正如您所看到的,我们在字符串`str1`的末尾添加了字符串`str2`。我们将参数传递给函数`strcpy`的顺序很重要,第一个参数指定输出字符串,而第二个参数指定附加在输出字符串末尾的另一个字符串。 正如文章开头所讨论的那样,该函数返回指向目标字符串的指针。这意味着如果我们显示此函数的返回值,那么它应该显示目标字符串(在我们的示例中为字符串`str1`)。让我们稍微修改我们的代码来验证这一点。 ```c #include <stdio.h> #include <string.h> int main () { char str1[50], str2[50]; //destination string strcpy(str1, "This is my initial string"); //source string strcpy(str2, ", add this"); //displaying destination string printf("String after concatenation: %s", strcat(str1, str2)); return(0); } ``` 输出: ```c String after concatenation: This is my initial string, add this ```