Thread: Scanf

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0

    Scanf


    I am new to C programming - what is a buffering and, what is a scanf buffer ?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    11
    Rep Power
    0
    and how does it work ? can you give an example ?
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,086
    Rep Power
    2222
    Buffering is necessary because computer I/O operates in real time and in an asynchronous manner. That means that input can come at any time, including when your program is not in any position to process it. To put that better, most input is virtually guaranteed to arrive when your program is busy doing something else. So what the computer does is to buffer that input. That is to say that the computer immediately receives the input, normally through an Interrupt Service Routine (ISR), and places it into a buffer. Then when your program finally gets around to processing input, it's waiting for your program in an input buffer. An analogy would be the in basket on your office desk: incoming work arrives while you're busy doing something else, so it goes into your in basket and waits until you are able to work on it.

    The input buffer for scanf goes a bit further, though. C and UNIX (the two were pretty much co-created) date back to circa 1970, when the computing paradigm was a mainframe computer that had many teletype terminals attached to it -- indeed, in UNIX/Linux terminal devices are designated as tty for "teletype". In general, these teletypes would allow you to compose an entire line of input and then send the entire line when you hit the Enter key; the only time they would send any input to the computer was in response to the Enter key being pressed. True, some teletypes would send each individual key when pressed, but the least common denominator was the single-line of input, so that is what C's standard input functions are based on. Therefore, scanf will not even begin to process any input until you have hit the Enter key.

    As each character is read from the buffer, it is removed from the buffer. Furthermore, scanf will only read as many characters as it needs to to perform the conversions that you specify in the format string, leaving any subsequent characters in the buffer for the next call to scanf. Since the newline character(s) from the Enter key is always left in the buffer, na´ve calls to read a single-character input will result in the newline character being read thus causing "inexplicable behavior" as the scanf call does not wait for the user to input the single-character command. This problem can be easily solved by telling scanf to ignore any preceding whitespace; ie with " %c" (note the leading space).
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,073
    Rep Power
    1802
    "buffering" in general refers to the temporary storage of data to compensate for different speeds of input an output.

    For example your keyboard input will generally be buffered so that the input data is stored until your program gets around to processing it.

    In most cases a computer program will respond quickly to user input, but I am sure you have seen occasions where your PC gets busy with some background operation and appears not to be responding, only to then process all your frustrated key-hits and mouse clicks all at once? That is buffering.

    Buffering is used everywhere where data is communicated between different systems operating or at least processing data at different rates, e.g. to and from a file system, for data communications, video streaming etc.

    Most buffers are FIFO (first-in-first-out) queues, meaning that the data is received in the order it was sent. It is also possible to have FILO (first-in-last-out) buffers (usually called a stack), or even a random access buffer where the reader and writer may access the data in any order (DMA buffers or shared memory buffers for example).

    In the case of scanf(), it is a stdio function usually associated with the system console (typically a keyboard and screen). While stdio may implement its own buffering, there may be other levels of buffering at the OS, device driver and even device level in the keyboard itself. Stdin (used by scanf) in particular is usually "line-buffered"; that is a function accepting input will not return until a complete line (ending in newline) has been entered, even if the function is waiting for a single character.

    You can see the effect of this buffering by writing code such as:

    Code:
    char data[256] ;
    scanf ( "%s", data ) ;
    puts( data ) ;
    scanf ( "%s", data ) ;
    puts( data ) ;
    If you then entered:

    Code:
    hello world
    The output would be:

    Code:
    hello
    world
    The whole input line is buffered before the first scanf returns, the first word is processed by the first scanf, the second remains buffered and is processed by the second scanf. The second scanf would not wait for new input because it can be satisfied by the buffered data.

    In some cases the array into which you capture data using scanf might be called a buffer, because often that is exactly what your program does, it saves the data for use later. However in this context is is often used in a looser sense as any multiple element storage (an array).
    Last edited by clifford; January 29th, 2013 at 01:54 PM.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,699
    Rep Power
    480
    A buffer is to computer program as warehouse is to store.

    Warehouses have maximum capacity.
    So do buffers.

    Thus we use
    fgets(array,sizeof(array),pointer_to_FILE);
    and carefully parse the data in the array (which is the buffer).


    buffer is to output as warehouse is to factory?
    buffer is to input as warehouse is to store?

    whoops! many answers already.

    (clifford should edit code please)

    char data[256] ;
    scanf ( "%s", &data ) ; /* not quite right */

    Comments on this post

    • clifford agrees : done
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,086
    Rep Power
    2222
    Originally Posted by b49P23TIvg
    (clifford should edit code please)

    char data[256] ;
    scanf ( "%s", &data ) ; /* not quite right */
    "The difference between a computer expert and a novice is that the novice hesitates before doing anything stupid." -- tag line

    As for myself, I let the compiler catch my many stupid mistakes. The problem is that when writing code for this forum, we usually do so without benefit of a compiler.

    Comments on this post

    • clifford agrees : Now I'm f---wit of the day!
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,073
    Rep Power
    1802
    [QUOTE=b49P23TIvg]A buffer is to computer program as warehouse is to store.

    Warehouses have maximum capacity.
    So do buffers.

    Thus we use
    fgets(array,sizeof(array),pointer_to_FILE);
    and carefully parse the data in the array (which is the buffer).


    buffer is to output as warehouse is to factory?
    buffer is to input as warehouse is to store?

    whoops! many answers already.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0
    The scanf() function is susceptible to buffer overflow. :)

IMN logo majestic logo threadwatch logo seochat tools logo