C - How to read a string with only spaces using scanf -
this question has answer here:
- how allow spaces entered using scanf? 11 answers
i'm little problem while doing excercise learn c. problem is: need read string user, if types space, need print space. that's okay in theory. but, when type space while program running, doesn't understand string , keeps waiting me type other things.
i'm using scanf("%[^\n]", string_name_here);
i appreciate help, , have nice day! o/ , sorry bad english, hope can understand :)
using char *fgets(char *str, int n, file *stream)
make day.
according man :
fgets() reads in @ 1 less size characters stream , stores them buffer pointed s. reading stops after eof or newline. if newline read, stored buffer. terminating null byte ('\0') stored after last character in buffer.
example program
#include <stdio.h> #define maxstr 21 int main(void) { char my_str[maxstr] = {'\0'}; fgets(my_str, maxstr, stdin); return 0; }
input :
claudio cortese
output :
claudio cortese
Comments
Post a Comment