代码 1
int main()
{
char str[]="abc";
char str1[]="hello computer";
strcat(str,str1);
printf("the concatenated string is : %s\n",str);
return 0;
}
输出-abchello computer
代码 2
int main()
{
char str[100]; //notice the change from code 1
char str1[]="hello computer";
strcat(str,str1);
printf("the concatenated string is : %s\n",str);
return 0;
}
输出-@#^hello computer
代码 3
int main()
{
char str[100];
char str1[]="hello computer";
strncpy(str,str1,5);
str[5]='\0'; //external addition of NULL
printf("the copied string is : %s\n",str);
return 0;
}
输出-hello
代码 4
int main()
{
char str[100]="abc";
char str1[]="hello computer";
strncat(str,str1,5);
printf("the concatenated string is : %s\n",str);
return 0;
}
输出-abchello
问题
Q-1) 为什么abchello computer
会显示 incode 1
和@#^hello computer
in code 2
?垃圾@#^
从哪里来?
Q-2) 为什么NULL '\0'
需要外部添加strncpy()
而不是分别strncat()
显示在code 3
和code 4
中?