Thread: confusion ragrding buffer

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    confusion ragrding buffer

    halo all,

    i just wanna know, am i correct in these thinkings regarding buffers.

    1. scanf keeps the newline in the buffer, so that the next time gets, fgets or any other char inputting functions are used they will read this newline kept in the buffer,resulting in unwanted behavoiur.is this true for getchar also.

    2.gets and fgets doesn't keep the characters in the buffer.

    hope ,you understand my questions.
    Thank You
    Last edited by BEN10; 04-04-2009 at 10:42 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by BEN10 View Post
    halo all,

    i just wanna know, am i correct in these thinkings regarding buffers.

    1. scanf keeps the newline in the buffer, so that the next time gets, fgets or any other char inputting functions are used they will read this newline kept in the buffer,resulting in unwanted behavoiur.is this true for getchar also.

    2.gets and fgets doesn't keep the characters in the buffer.

    hope ,you understand my questions.
    Thank You
    1) No, scanf() keeps the newline in the keyboard buffer *if* the scanf() is taking in a single char. (because there's obviously no room for a newline to squeeze in).

    If scanf() is taking in a number, it will also leave the newline.

    If it is taking in a string, scanf() will put the newline at the end of the string, if there is room. If there is no room in the string buffer for the newline, it overwrites something in memory, but it still takes it.

    2) gets() and fgets() will take the newline into the char string - gets() always, again overwriting whatever if there is not enough room - fgets() will take the newline only if there is room for it in the char string. Otherwise, it is left in the keyboard buffer, if you've set your numbers for it to take, correctly. If you haven't (sized it too big), it's just like gets().

    This is how I understand it, but it certainly is *not* a definitive answer.

    If the following scanf() is for a number, then a remaining newline will be "skipped" as whitespace.

    If the following scanf() is for a char, the newline will be read as that char, and result in it seeming to skip the scanf() completely.

    I believe scanf(), when it's expecting a string, and finds a newline still in the keyboard buffer, will skip over the newline and take the string of char's that is behind it, in the keyboard buffer.
    Last edited by Adak; 04-04-2009 at 11:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  4. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 11:12 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM