Thread: what happens after 'fgets' and 'scanf'

  1. #1
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51

    what happens after 'fgets' and 'scanf'

    Hey people!!!

    Bit of a simple question for you all but it might solve a few
    porblems for me.

    basially, i want to know if the '\n' character is put into the buffer
    everytime i press enter after a 'scanf' or 'fgets' .etc

    And if this is the case do i need to leave two extra characters to
    house the resulting '\n' and '\0' characters?

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by the bassinvader
    Hey people!!!

    Bit of a simple question for you all but it might solve a few
    porblems for me.

    basially, i want to know if the '\n' character is put into the buffer
    everytime i press enter after a 'scanf' or 'fgets' .etc

    And if this is the case do i need to leave two extra characters to
    house the resulting '\n' and '\0' characters?
    From manpages:
    Code:
    s      Matches  a  sequence  of  non-white-space  characters;  the next
                  pointer must be a pointer to character array that is long enough
                  to  hold  the  input sequence and the terminating null character
                  (’\0’), which is added automatically.  The input string stops at
                  white  space  or  at  the  maximum field width, whichever occurs
                  first.
    Fgets
    Code:
    fgets() reads in at most one less than size characters from stream  and
           stores  them  into  the buffer pointed to by s.  Reading stops after an
           EOF or a newline.  If a newline is read, it is stored into the  buffer.
           A ’\0’ is stored after the last character in the buffer.
    Last edited by Maragato; 07-30-2006 at 02:34 PM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >basially, i want to know if the '\n' character is put into the buffer
    >everytime i press enter after a 'scanf' or 'fgets' .etc
    scanf is finicky with whitespace (it usually acts as an unread delimiter), and fgets stores the newline if possible.
    My best code is written with the delete key.

  4. #4
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51
    so if the string isnt full up after the input, fgets will put the '\n' but if there is no space it's forgotten?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > but if there is no space it's forgotten?
    No, it's left for the next input call (say another fgets) to deal with.

    Mostly it's not important if you pick a reasonably large buffer size. If it is, you can always look for the \n and take appropriate action.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf vs fgets?
    By Matus in forum C Programming
    Replies: 65
    Last Post: 11-17-2008, 04:02 PM
  2. is scanf as safe as fgets when used to receive string?
    By Antigloss in forum C Programming
    Replies: 4
    Last Post: 08-31-2005, 05:18 PM
  3. replacing scanf with fgets
    By subflood in forum C Programming
    Replies: 6
    Last Post: 08-19-2004, 01:59 PM
  4. replacing scanf with fgets
    By guest73 in forum C Programming
    Replies: 8
    Last Post: 09-16-2002, 04:52 AM
  5. String manipulation and scanf vs fgets
    By Nit in forum C Programming
    Replies: 9
    Last Post: 03-20-2002, 12:44 PM