温馨提示×

c语言如何把字符串连接

小亿
138
2023-08-15 02:34:14
栏目: 编程语言

在C语言中,可以使用strcat函数将两个字符串连接起来。

strcat函数的原型如下:

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

其中,destination是目标字符串,source是要连接的字符串。

使用strcat函数的示例代码如下:

#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[] = " World!";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}

输出结果:

Concatenated string: Hello World!

以上代码将字符串"Hello"和" World!"连接起来,结果存储在str1中。

0