user7030669 user7030669 - 1 month ago 10
C Question

stuck with spaces and arrays

I want to display a full name, but I can't enter more than two parts of a name. The program stuck when enter a name which has more characters than the number which array has. How can I solve this?

#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;
}


Thank You

Answer

I think this can help you. This program doesnt care how many characters, spaces you entered. It only displays first 24 characters and spaces. (1 for string terminator)

#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; 
  }