c语言怎么拼接两个字符串
在C语言中,可以使用strcat()
函数来拼接两个字符串。strcat()
函数将第二个字符串连接到第一个字符串的末尾,并返回连接后的字符串。以下是一个示例代码:
#include <stdio.h>#include <string.h>int main() {char str1[50] = "Hello, ";char str2[] = "world!";strcat(str1, str2); // 拼接两个字符串printf("拼接后的字符串: %s\n", str1);return 0;}
在上面的代码中,首先定义了两个字符串str1
和str2
,然后使用strcat()
函数将str2
连接到str1
的末尾,并将结果打印输出。运行代码后,输出结果为Hello, world!
。