Author: 刘晨晖
#include <stdio.h>
#include <string.h>
int main()
{
char s1[30]="hello",s2[]="programs";
// s1[30] 的大小一定要限制,并且要足够大,否则内存溢出,会出现 Segmentation fault
strcat(s1,s2);
printf("%s/n",s1); // 输出 helloprograms
return 0;
}
本文提供了一个使用C语言进行字符串连接的简单示例。通过使用标准库函数`strcat()`,演示了如何将两个字符串合并成一个。需要注意的是,在进行字符串操作时,确保目标数组有足够的空间来容纳新的字符串内容,以避免内存溢出导致程序崩溃。
Author: 刘晨晖
#include <stdio.h>
#include <string.h>
int main()
{
char s1[30]="hello",s2[]="programs";
// s1[30] 的大小一定要限制,并且要足够大,否则内存溢出,会出现 Segmentation fault
strcat(s1,s2);
printf("%s/n",s1); // 输出 helloprograms
return 0;
}
2万+

被折叠的 条评论
为什么被折叠?