温馨提示×

c语言怎么将两个字符串拼接

小亿
191
2024-04-26 14:04:47
栏目: 编程语言
C语言开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C语言中,可以使用strcat函数将两个字符串拼接在一起。例如:

#include <stdio.h>
#include <string.h>

int main() {
    char str1[20] = "Hello";
    char str2[] = "World";
    
    strcat(str1, str2);
    
    printf("Concatenated string: %s\n", str1);
    
    return 0;
}

运行上面的程序将输出:

Concatenated string: HelloWorld

注意,使用strcat函数时要确保第一个字符串的数组大小足够大,以容纳拼接后的新字符串。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:c语言如何拼接两个字符串

0