fflush(stdin), scanf and a space

Discussion in 'C Programming' started by barry, Jun 7, 2010.

  1. barry

    barry Guest

    Recently I came across following program:

    #include<stdio.h>
    int main()
    {
    int a;
    scanf("%d",&a);
    printf("%d",a);
    scanf(" %d",&a); // note the space b/w " and %
    printf("%d",&a);

    }

    There was no problem with this program
    but the moment I removed the space in second scanf, program was
    exiting before taking input of a in second scanf.
    I think it was due to buffered carriage return in stdin file stream
    I inserted fflush(stdin) above second scanf to rectify it.
    I have also read 2 days ago that fflush(stdin) is not allowed
    according to C standard and has undefined behavior.

    I was thinking about a work around for equivalent of fflush(stdin).

    I want to know that whether above trick of leaving a space before
    format string in scanf for clearing stdin stream is reliable or not?
     
    barry, Jun 7, 2010
    #1
    1. Advertising

  2. barry

    Seebs Guest

    On 2010-06-07, barry <> wrote:
    > I have also read 2 days ago that fflush(stdin) is not allowed
    > according to C standard and has undefined behavior.


    Yup.

    > I was thinking about a work around for equivalent of fflush(stdin).


    It's pretty hard to define.

    Start at the top: What exactly do you want "flushed" or "discarded"?
    How do you know, looking at a character, whether or not you want it
    to be discarded? Do you want to discard everything up to the next newline?

    If you want to discard "everything which was in the buffer already", that's
    not possible to do portably/safely. (I usually look at the BSDish "fpurge()"
    for that.) However, it's also often a bad idea, because on many systems,
    input could be a file, in which case you'd be discarding an entire disk
    block's worth of data usually, and that's almost never what you want.

    A typical solution:

    while ((c = getchar()) != EOF && c != '\n')
    ;

    This discards everything up to the next newline.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Jun 7, 2010
    #2
    1. Advertising

  3. barry

    Fred Guest

    On Jun 7, 1:14 pm, barry <> wrote:
    > Recently I came across following program:
    >
    > #include<stdio.h>
    > int main()
    > {
    >         int a;
    >         scanf("%d",&a);
    >         printf("%d",a);
    >         scanf(" %d",&a);         // note the space b/w " and %
    >         printf("%d",&a);
    >
    > }
    >
    > There was no problem with this program
    > but the moment I removed the space in second scanf, program was
    > exiting before taking input of a in second scanf.
    > I think it was due to buffered carriage return in stdin file stream
    > I inserted fflush(stdin) above second scanf to rectify it.
    > I have also read 2 days ago that fflush(stdin) is not allowed
    > according to C standard and has undefined behavior.
    >
    > I was thinking about a work around for equivalent of fflush(stdin).
    >
    > I want to know that whether above trick of leaving a space before
    > format string in scanf for clearing stdin stream is reliable or not?


    Either your compiler is broken, or what you posted is not what your
    code
    really looks like. Other than the missing "\n" in the printf
    statements, and not checking for input errors, it ought to work.

    The newline still in the input buffer is whitespace; the second scanf
    should try to read a new integer, ignoring any leading whitespace.
    --
    Fred K
     
    Fred, Jun 7, 2010
    #3
  4. barry

    Eric Sosman Guest

    On 6/7/2010 4:14 PM, barry wrote:
    > Recently I came across following program:
    >
    > #include<stdio.h>
    > int main()
    > {
    > int a;
    > scanf("%d",&a);
    > printf("%d",a);
    > scanf(" %d",&a); // note the space b/w " and %
    > printf("%d",&a);
    >
    > }
    >
    > There was no problem with this program
    > but the moment I removed the space in second scanf, program was
    > exiting before taking input of a in second scanf.
    > I think it was due to buffered carriage return in stdin file stream


    I don't think so. The "%d" specifier, all by itself, will skip
    any leading white space it finds, and in the default "C" locale all
    of space, tab, newline, carriage return, form feed, and vertical tab
    count as white space. The leading " " would skip exactly the same
    set of characters, so its presence or absence should make no difference.

    It might be useful to know (1) what the input was, (2) what values
    the scanf() calls returned, and (3) what values were stored in `a'.

    > I inserted fflush(stdin) above second scanf to rectify it.


    Bad idea.

    > I have also read 2 days ago that fflush(stdin) is not allowed
    > according to C standard and has undefined behavior.


    Right. (Which is why using it was a bad idea.)

    > I was thinking about a work around for equivalent of fflush(stdin).


    Any of

    *((double*)314159) = 3.14159;
    free("Goodbye, cruel world!");
    "spelling erryr"[12] = 'o';
    printf ("%s");

    (and many others) are rough equivalents, as they all produce undefined
    behavior.

    > I want to know that whether above trick of leaving a space before
    > format string in scanf for clearing stdin stream is reliable or not?


    A white-space character (any white-space character) in the format
    string will match and ignore zero or more white-space characters (any
    white-space characters) in the input, reading until it finds something
    non-white or runs out of things to read.

    ... but, as mentioned above, since plain "%d" skips leading white
    space anyhow, that should have made no difference. More details,
    please!

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jun 7, 2010
    #4
  5. barry <> wrote:
    > Recently I came across following program:
    >
    > #include<stdio.h>
    > int main()
    > {
    >         int a;
    >         scanf("%d",&a);


    You should check the return value of scanf.

    >         printf("%d",a);
    >         scanf(" %d",&a);         // note the space b/w " and %


    The space is redundant as %d will ignore leading whitespace,
    including new-lines anyway.

    >         printf("%d",&a);


    Did you really mean to print the address?

    > }
    >
    > There was no problem with this program
    > but the moment I removed the space in second scanf,
    > program was exiting before taking input of a in
    > second scanf.


    I'm doubting this is the actual code.

    > I think it was due to buffered carriage return in stdin
    > file stream I inserted fflush(stdin) above second scanf
    > to rectify it.


    All you did was invoke undefined behaviour since fflush()
    is only defined for output streams.

    > I have also read 2 days ago that fflush(stdin) is not
    > allowed according to C standard and has undefined behavior.


    Correct.

    > I was thinking about a work around for equivalent of
    > fflush(stdin).


    First tell us what you think fflush(stdin) is supposed to
    do.

    > I want to know that whether above trick of leaving a space
    > before format string in scanf for clearing stdin stream is
    > reliable or not?


    As stated, it adds nothing.

    If you want to read a line, use fgets and scan the string
    using sscanf or strtoxxx.

    --
    Peter
     
    Peter Nilsson, Jun 8, 2010
    #5
  6. barry

    Eric Sosman Guest

    On 6/9/2010 12:29 PM, io_x wrote:
    > [... something incoherent ...]
    > i'm like one horse i like freedom


    You're not all that much like a horse. Horses are considerably
    larger than people, usually walk on four legs rather than on two,
    bear infants that can stand upright within a few hours of birth, and
    so on. Few people viewing pictures of io_x and of a horse would have
    difficulty in telling which was which, provided the horse's picture
    showed the end with the head.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jun 9, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,914
    Bryan Bullard
    Jul 11, 2003
  2. hugo27
    Replies:
    5
    Views:
    639
    Chris Torek
    Oct 10, 2004
  3. asit

    fflush and stdin

    asit, Jan 14, 2008, in forum: C Programming
    Replies:
    17
    Views:
    845
    Randy Howard
    Jan 20, 2008
  4. Tagore

    fflush(stdin), scanf and a space

    Tagore, Dec 28, 2008, in forum: C Programming
    Replies:
    5
    Views:
    4,887
    Eric Sosman
    Dec 29, 2008
  5. jaswinder

    fflush not flusing stdin!

    jaswinder, Aug 18, 2010, in forum: C Programming
    Replies:
    4
    Views:
    1,200
    Jens Thoms Toerring
    Aug 19, 2010
Loading...

Share This Page