我想显示一个全名,但我不能输入一个名字的两个以上部分。当输入的名称字符数多于数组的字符数时,程序卡住了。我该如何解决这个问题?
#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;
}
谢谢
最佳答案
我想这对你有帮助。该程序不关心您输入了多少个字符、空格。它只显示前 24 个字符和空格。 (1 个用于字符串终止符)
#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;
}
关于c - 卡在空格和数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40087329/