C 被空格和数组卡住了
我想显示全名,但不能输入超过两部分的名称。当输入的名称的字符数大于数组中的数字时,程序会卡住。我怎样才能解决这个问题C 被空格和数组卡住了,c,arrays,C,Arrays,我想显示全名,但不能输入超过两部分的名称。当输入的名称的字符数大于数组中的数字时,程序会卡住。我怎样才能解决这个问题 #include <stdio.h> #include<stdlib.h> int main(){ char x[25]; printf("Enter your name"); scanf("%s",x); printf("Your name is %s", x); return 0; } #包括 #包括 int main(){ charx[25];
#include <stdio.h>
#include<stdlib.h>
int main(){
char x[25];
printf("Enter your name");
scanf("%s",x);
printf("Your name is %s", x);
return 0;
}
#包括
#包括
int main(){
charx[25];
printf(“输入您的姓名”);
scanf(“%s”,x);
printf(“您的名字是%s”,x);
返回0;
}
谢谢你我想这对你有帮助。这个程序不在乎你输入了多少个字符和空格。它只显示前24个字符和空格。(1个用于字符串终止符)
#包括
#包括
int main(){
charx[25];
char*xx=x;
puts(“输入名称”);
fgets(xx,25,标准DIN);
卖出(xx);
返回0;
}
scanf(“%24[^\n]”,x)
这是建议使用fgets
而不是scanf
的几个原因之一。请正确格式化/缩进您的程序。@BLUEPIXY它可以工作!谢谢,xx
变量的作用是什么?您可以直接使用x
。谢谢!什么是字符串终止符?字符串终止符存储为数组,它是unprintable@user7030669-根据定义,a是一个始终以空终止符结尾的“char”数组,形式为“\0”或“0”。这就是为什么如果需要显示25个字符的字符串,字符串数组必须包含26个字符的空间。
#include <stdio.h>
#include <stdlib.h>
int main(){
char x[25];
char *xx=x;
puts("Input Name");
fgets(xx,25,stdin);
puts(xx);
return 0;
}