main() {
int x = 0;
char s1[5] = "abc";
int y = 0;
char s2[] = "defghijklmno";
strcat(s1, s2);
printf("x = %d, s1 = '%s', y = %d\n", x, s1, y);
}
請你試試執行的結果.
又如下面的例子, 如果使用者輸入的字串很長, 也會讓程式出現錯誤.
main() {
int x = 0;
char s1[5];
int y = 0;
printf("Please input a string with it's length smaller than 5:");
scanf("%s", s1);
printf("x = %d, s1 = %s, y = %d\n", x, s1, y);
}
解決此問題的方法是告訴scanf可以輸入的上限
main() {
int x = 0;
char[5] s1;
int y = 0;
printf("Please input a string with it's length smaller than 5:");
scanf("%4s", s1);
printf("x = %d, s1 = %s, y = %d\n", x, s1, y);
}