【发布时间】:2021-12-17 14:11:03
【问题描述】:
我正在尝试练习声明字符串,但我没有得到正确的输出。
#include <stdio.h> //Im using vscode and gcc
int main()
{
char k[]= "prac c";
char l[6]= "stop c"; //first initializing as size 6
char m[6]= "nice c";
printf("%s \n%s \n%s",k,l,m);
return 0;
}
output: prac c
stop cprac c
nice cstop cprac c
但是当我将大小从 6 更改为 7 时,这不会发生。
#include <stdio.h>
int main()
{
char k[]= "prac c";
char l[7]= "stop c"; //changing size to 7
char m[7]= "nice c"; //changing size to 7
printf("%s \n%s \n%s",k,l,m);
return 0;
}
output: prac c
stop c
nice c
【问题讨论】:
-
开头还有这个奇怪的空格,你在
printf打印一个空格。此外,l和m的大小必须为7而不是6-NULL('\0') 终止符要多出一个字符。 -
@kiner_shah 感谢它的工作,我刚刚意识到字符串的大小将是一个额外的。