头文件<stdio.h>里包含了字符串连接函数strcat()和strncat()的声明。两个函数的原型方别是:
char *strcat(char *destin, char *source); //将字符串source连接到字符串destin后边。
#include <stdio.h>
#include<conio.h>
int main(void)
{
char s1[] = "1234";
char s2[] = "abcdefg" ;
strcat(s1,s2);
printf("%s\n", s1);
getch();
return 0;
}
输出结果:1234abcdefg
char *strncat(char *destin,char *source ,int n) //将字符串source前n个字符连接到字符串destin后边。
#include <stdio.h>
#include<conio.h>
int main(void)
{
int n = 3;
char s1[] = "1234";
char s2[] = "abcdefg" ;
strncat(s1, s2, n);
printf("%s\n", s1);
getch();
return 0;
}
输出结果:1234abc;
在使用strcat()函数与strncat()函数时可以不考虑是否会溢出,strcat()函数与strncat()函数会重新分配存储空间。
#include <stdio.h>
#include<conio.h>
int main(void)
{
int n = 3;
char s1[5] = "1234";
char s2[] = "abcdefg" ;
strncat(s1, s2, n);
printf("%s\n", s1);
getch();
return 0;
}
输出结果依旧是:1234abc
#include <stdio.h>
#include<conio.h>
int main(void)
{
int n = 3;
char s1[5] = "1234";
char s2[] = "abcdefg" ;
strcat(s1, s2);
printf("%s\n", s1);
getch();
return 0;
}
输出结果依旧是:1234abcdefg