Thread: scanf()

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    scanf()

    How does this expression work? Basically the \n in scanf()

    Code:
    int main()
    {
    int i;
    scanf("%d\n",&i);
    }
    Is there any article which gives a detailed working of scanf()? Why many times the enter key remains in the buffer while other times it gets removed automatically??
    Last edited by juice; 12-20-2011 at 04:19 AM.

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Code:
    #include<stdio.h>
    int main()
    {
    char c,d;
    scanf("%c",&c);
    scanf("%c",&d);
    }
    The enter key remains in the buffer for the above code, but it gets flushed automatically while receiving integers as in the code below..

    Code:
    int main()
    {
    int i,j;
    scanf("%d",&j);
    scanf("%d",&i);
    }

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    Philippines
    Posts
    15
    Code:
    #include <stdio.h>
    
    
    int main()
    {
    int i;
    scanf("%d\n",&i);
    printf("%d", i);
    getch();
    }
    in this code, you can will enter 2 integers first before it can output the first
    Code:
    #include <stdio.h>
    
    
    int main()
    {
    int i;
    scanf("%d",&i);
    printf("%d", i);
    getch();
    }
    unlike with this, it will automatically print the value of i.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by juice View Post
    The enter key remains in the buffer for the above code, but it gets flushed automatically while receiving integers as in the code below..
    Almost. Scanf functions will eat any amount of leading whitespace (\t, \n, space) with numerical types by default, but it can't do that with character or string types. So the newline actually did remain in the buffer -- it got flushed by the second scanf() because you were looking for a number.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    any place where I could get the complete working of scanf, cause google ain't helping.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    That's strange, it helped me find this link: scanf(), the third link.

    Jim

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jimblumberg View Post
    That's strange, it helped me find this link: scanf(), the third link.

    Jim
    Hmm, and it looks like I was a wrong when I said it doesn't skip whitespace for %s:

    Input white-space characters (as specified by isspace()) shall be skipped, unless the conversion specification includes a [, c, C, or n conversion specifier.
    Which is verbatim from the C99 draft, "7.13.6.2 The fscanf function".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    any place where I could get the complete working of scanf, cause google ain't helping.
    Did you look in your Pelles C Help file? It's all there...

    Seriously... juice... how many times do you need to be told this?

    Open POIDE... with your source code showing... click on the word scanf then press F1...

    You will get this...

    scanf()-scanf1-png

    Part way down the page there is a link to fscanf, which gives you the rest of the story...

    scanf()-scanf2-png

    There you have everything scanf() can do in Pelles C.

    And really... what is it with this stupid aversion people have to help files?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Some rules:
    1. Except for % conversions, each character stands for itself. So if you have scanf("abc"), you need to type in "abc" to proceed.
    2. All white space characters (\n, \t, space etc) match any combination (and any length) of any characters where isspace() is true.
    3. Skipping leading whitespace is for free - except as noted in post #7.
    4. scanf stops when it a) reaches the end of the string, or b) when the input no longer matches the control string.

    The consequence of all this is that if you have scanf("%d\n", &var); then you can enter 123 (say) AND as many spaces, tabs and newlines as you like. It will ONLY return when you type in some other non-whitespace character. All the whitespace will disappear from the input stream, and the first non-whitespace will be left on the input for the next input function to deal with.
    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.

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by Salem View Post
    Some rules:
    1. Except for % conversions, each character stands for itself. So if you have scanf("abc"), you need to type in "abc" to proceed.
    2. All white space characters (\n, \t, space etc) match any combination (and any length) of any characters where isspace() is true.
    3. Skipping leading whitespace is for free - except as noted in post #7.
    4. scanf stops when it a) reaches the end of the string, or b) when the input no longer matches the control string.

    The consequence of all this is that if you have scanf("%d\n", &var); then you can enter 123 (say) AND as many spaces, tabs and newlines as you like. It will ONLY return when you type in some other non-whitespace character. All the whitespace will disappear from the input stream, and the first non-whitespace will be left on the input for the next input function to deal with.
    That's exactly what I wanted. Thanks.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by CommonTater View Post
    Did you look in your Pelles C Help file? It's all there...
    lately I've started to realize that the help file is indeed a helpful resource, but what salem told could not be found in the help file.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    lately I've started to realize that the help file is indeed a helpful resource, but what salem told could not be found in the help file.
    Yes it is... look at the image in Message #8 ... most of what Salem said is right there...

    And what is this resistence to using Help files all about?

    Y'know what... Accurately describing your program's behaviour to someone who's never seen it before is often a bigger and more difficult challenge that writing the program itself.

    People don't write those things for own amusement, you know
    Last edited by CommonTater; 12-20-2011 at 11:25 PM.

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by CommonTater View Post
    Yes it is... look at the image in Message #8 ... most of what Salem said is right there...
    Yes, most of it indeed is given in the help article for fscanf, I didn't read that. Thanks

  14. #14
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Why does this statement terminate as soon as enter key is hit? Isn't it supposed to receive two multiword strings and store them in str1 and str2??

    Code:
    scanf("%[^\n]s %[^\n]s",str1,str2);

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by juice
    Why does this statement terminate as soon as enter key is hit?
    What does [^\n] mean?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with scanf...
    By vsovereign in forum C Programming
    Replies: 7
    Last Post: 03-01-2010, 11:29 AM
  2. gets? scanf?
    By msshapira in forum C Programming
    Replies: 4
    Last Post: 01-07-2009, 03:53 PM
  3. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  4. Help with scanf
    By newprog in forum C Programming
    Replies: 6
    Last Post: 01-29-2003, 09:12 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