您的位置:宽带测速网 > 编程知识 > c语言如何把字符串连起来

c语言如何把字符串连起来

2025-06-21 20:40来源:互联网 [ ]

在C语言中,可以使用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;}

在这个示例中,str1是第一个字符串,str2是第二个字符串。通过调用strcat()函数,将str2连接到了str1的末尾,最终输出结果为"Hello, world!"