Why is it that if i use scanf for input of a string data type, the words entered after a space are not printed?
Why is it that if i use scanf for input of a string data type, the words entered after a space are not printed?
Because scanf (with a basic %s format) will see spaces as "string separators".
I'd suggest that you use fgets() to read strings with spaces. It is much easier and more safe than trying to do it with scanf().
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
You can also use a custom conversion specifier like %[ A-Za-z] which will capture uppercase letters, lower case letter, and spaces. %[ A-Za-z*!;] will capture all that plus *, ! or ;
etc.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
to read string using scanf till the \n char the following format could be used
"%[^\n]"
of course adding width specifier will be good practice as well
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler