Like Tree5Likes

scanf()

This is a discussion on scanf() within the C Programming forums, part of the General Programming Boards category; How does this expression work? Basically the \n in scanf() Code: int main() { int i; scanf("%d\n",&i); } Is there ...

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

    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 03:19 AM.

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    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.
    rags_to_riches likes this.
    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
    384
    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
    2,753
    That's strange, it helped me find this link: scanf(), the third link.

    Jim
    MK27 and inu11byte like this.

  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...

    Name:  scanf1.PNG
Views: 610
Size:  80.4 KB

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

    Name:  scanf2.PNG
Views: 418
Size:  97.6 KB

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    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.
    juice likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    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
    384
    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 10:25 PM.

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    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
    384
    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
    21,720
    Quote Originally Posted by juice
    Why does this statement terminate as soon as enter key is hit?
    What does [^\n] mean?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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, 10:29 AM
  2. gets? scanf?
    By msshapira in forum C Programming
    Replies: 4
    Last Post: 01-07-2009, 02: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, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21