What happened when I put \n or any white-space character at the end of scanf function?
Why the compiler "Hang" until I press "enter" button to print the inserted data?
Thank you
What happened when I put \n or any white-space character at the end of scanf function?
Why the compiler "Hang" until I press "enter" button to print the inserted data?
Thank you
Last edited by The coder; 11-27-2015 at 02:31 AM.
Because you're asking scanf to match the character (or sequence) '\n'?
Henny Youngman, was a comedian known for his "One-Liners". One of my favorites, that I have used in my courses:
I don't think the Standards even mention it, but...Patient: Doctor, Doctor, it hurts when I do this!
Doctor: Don't do it!
The use of '\n', the newline character constant in the format string for an of the scanf() functions, is NOT recommended. You have enough problems dealing with the trailing newline chars in the input string, newlines left in the buffer after accepting valid input, or bad input such as asking for an int, and having the user type in "a12", "abc", etc... I cannot tell you all the various effects of putting the '\n' char in the format string, as I would never do this, tested this extensively, nor do I use scanf() in real life code.
There are better ways of inputing data than scanf(), such as receiving the whole line using fgets(), including the trailing newline, or accessing the data in the buffer, one character at a time.