坚持使用空格和数组

时间:2016-10-17 13:12:02

标签: 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;
}

谢谢

1 个答案:

答案 0 :(得分:2)

我认为这可以帮到你。该程序不关心您输入的字符数,空格。它只显示前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; 
  }