Thread: The function scanf is notorious for its poor end-of-line handling.

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    61

    The function scanf is notorious for its poor end-of-line handling.

    Greetings EveryOne

    I have read this in a book:

    The function scanf is notorious for its poor end-of-line handling.
    What does that mean, how come scanf function handles end-of-line poorly?

    Thanks In Advance
    Last edited by Laythe; 03-25-2011 at 12:49 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Because it leaves the CR/LF pair in the input buffer... But there's a real easy fix for that...

    (No, don't ask us what it is.... look in your compiler documentation and see if you can find it yourself.)

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    Thank you for your reply

    what does CR/LF stands for?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Laythe View Post
    Thank you for your reply

    what does CR/LF stands for?
    Carriage Return / Line Feed ... the end of line markers in text files.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    I'm using CodeBlocks and the version i downloaded is not supplied with documentation what do you advice me to do? change another IDE maybe?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well... at the very least find library documentation for it... (It's MinGW by the way)

    In the mean time here's a general C99 documentation page to get you started...
    Dinkum C99 Library

    Another IDE? Hmmm... if you are working only in C, I'd recommend Pelles C It's much more complete than Code:Blocks and unlike MinGW, it's fully documented. In fact it has one of the best help files I've ever seen.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Code::Blocks is not a compiler, it is an Integrated Development Environment. The compiler is probably gcc which is well documented. You can start here.

    Also the CR/LF pair is only the end of line markers for DOS/Windows operating systems. Other operating systems may use the CR or LF by themselves.


    Jim

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    can someone show an illustration of how does scanf function leaves the CR/LF pair in the input buffer, or more explanation please.

  9. #9

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The scanf family of functions require specifically formatted input. That's what the f in scanf means. That means it will only read exactly what you tell it to read.

    It will read the newline marker, if you tell it to expect it. Otherwise it won't.
    Code:
    scanf( "%c", &onechar );
    This says "read one character". That's all. So if you type 'Y' for a yes/no prompt, and hit enter, you've actually just input two keys. But you've only told it to read one. So it grabs the first key, the 'Y', and the remaining key is still in the input stream, waiting for you to do something with it.

    Input streams in C are by default line buffered, which means that the function only fires when you hit enter. This means you can't by default make a program that reads every key stroke. You can't just press 'Y' and have it read that. It waits for you to hit enter, then it passes everything prior to your hitting enter to whatever you are using.

    If you've told scanf to expect a space and then a character, it will check and see if there's a space waiting, if so, it will then grab the next character. If you tell it to expect a floating point number, it will start picking through the start of the stream to see if what's waiting there matches what a floating point number should look like.

    It wants you to input exactly what you tell it to input. Anything that doesn't match makes it not pull that from the input stream.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    Thank you so very much Mr.quzah for the good explanation

    This says "read one character". That's all. So if you type 'Y' for a yes/no prompt, and hit enter, you've actually just input two keys. But you've only told it to read one. So it grabs the first key, the 'Y', and the remaining key is still in the input stream, waiting for you to do something with it.
    the remaining key in this case is the enter key is that right, input stream is waiting for me to do something like what with the remaining key?

    It wants you to input exactly what you tell it to input. Anything that doesn't match makes it not pull that from the input stream.
    in this code example:

    Code:
    i#include <stdio.h>
    
    int main()
    {
        int  Integer = 0;
    
        printf("Please Enter An Integer: ");
    
        scanf(" %d", &Integer);
    
        printf("Twice %d Is %d\n", Integer, Integer*2);
    
        return 0;
    }
    at runtime, even if you don't enter a space before entering an integer the program run normally without any problem, so is there any problem occurred at unseen level?

    same thing if you enter multiple space key before entering the integer, the program run and ends normally, any comment about that too?

    Thank you for your help, it's very appreciated ^_^

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should probably read the man page for the details. There's no point in me retyping the whole thing here.

    Read the description and the section below the description which explains all of the format specifiers.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    thank you for the link it's very informative but still i couldn't find the answer to my previous question within that page or did i miss it!

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You missed it, in the description:
    Each successive pointer argument must correspond properly with
    each successive conversion specifier (but see the * conversion below).
    All conversions are introduced by the % (percent sign) character. The
    format string may also contain other characters. White space (such as
    blanks, tabs, or newlines) in the format string match any amount of white
    space, including none, in the input. Everything else matches only
    itself. Scanning stops when an input character does not match such a
    format character. Scanning also stops when an input conversion cannot be
    made (see below).

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    looks like i didnt miss it but i dindnt understood it well

    White space in the format string match any amount of white
    space, including none, in the input.
    does that means white space is ignored scanf?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an scanf function inside an function
    By anserudd in forum C Programming
    Replies: 4
    Last Post: 03-25-2011, 09:19 AM
  2. can't compile library
    By pjs111 in forum C Programming
    Replies: 16
    Last Post: 05-24-2010, 01:42 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM