Thread: Putting /n at the end of "scanf" function

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    7

    Question Putting /n at the end of "scanf" function

    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.

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Because you're asking scanf to match the character (or sequence) '\n'?

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Henny Youngman, was a comedian known for his "One-Liners". One of my favorites, that I have used in my courses:
    Patient: Doctor, Doctor, it hurts when I do this!
    Doctor: Don't do it!
    I don't think the Standards even mention it, but...

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 31
    Last Post: 11-25-2009, 01:10 PM
  2. Replies: 4
    Last Post: 07-02-2004, 04:08 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

Tags for this Thread