fflush(stdin), scanf and a space

Discussion in 'C Programming' started by Tagore, Dec 28, 2008.

  1. Tagore

    Tagore 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?
     
    Tagore, Dec 28, 2008
    #1
    1. Advertising

  2. Tagore <> writes:

    > Recently I came across following program:
    >
    > #include<stdio.h>
    > int main()


    int main(void) is better.

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


    This is wrong (undefined behaviour). Have you re-typed? It is always
    better to get the real code into your post if you can. You may not
    see the output unless you end with a newline and it is better to
    return 0; at the end of main (it is permitted to omit the return in
    C99).

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


    Sounds odd. Care to name and shame the compiler/library that you are
    using?

    > I think it was due to buffered carriage return in stdin file stream


    It sounds like an error in the scanf implementation to me.

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


    Yes, it is undefined so that is not a solution. I'd suggest one but
    if scanf is going wrong, it is very hard to guess what will fix it.

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


    I don't see the need if leaving the space in there happens to work.
    It should not make any difference, but since it does, why not just
    leave it there?

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


    I don't know what you mean by "clearing" but you should be able to
    rely on the specified behaviour of scanf: any white space in the
    format causes scanf to read up to the next non-space character,
    leaving it in the stream. This happens with %d anyway, which is why
    "%d" and " %d" should be the same. If scanf is not doing what it
    should then you are in a strange situation and it is hard to give
    practical advice.

    --
    Ben.
     
    Ben Bacarisse, Dec 29, 2008
    #2
    1. Advertising

  3. Andrew McMeikan <> writes:

    > Tagore 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?
    >>

    >
    > If it works it is probably a good way of doing it, I recently had
    > same problem with fgets


    I don't think you had the same problem. The OP is describing
    something that sounds like an error in scanf. They posted code that
    has other errors so maybe the report is wrong but so far the only
    information is that something is broken in scanf. If I remember your
    code, the behaviour of the library functions was correct.

    >... and used following
    >
    > void flushstdin(void){
    > if (!feof(stdin)) {
    > getc(stdin);
    > }
    > }


    That does something else altogether! Inserting that between the scanf
    calls in the OP's code it would cause +1-2 to be read as 1 and 2 rather
    than 1 and -2. It seems a dangerous suggestion to throw away an input
    character.

    > but for scanf the space seems neat.


    Yes, it has its uses if scanf is doing what it should. However that
    space should not matter in this particular case.

    --
    Ben.
     
    Ben Bacarisse, Dec 29, 2008
    #3
  4. Tagore 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?
    >


    If it works it is probably a good way of doing it, I recently had
    same problem with fgets and used following

    void flushstdin(void){
    if (!feof(stdin)) {
    getc(stdin);
    }
    }
    but for scanf the space seems neat.

    cya, Andrew...
     
    Andrew McMeikan, Dec 29, 2008
    #4
  5. Tagore

    CBFalconer Guest

    Ben Bacarisse wrote:
    >

    .... snip ...
    >
    > I don't know what you mean by "clearing" but you should be able
    > to rely on the specified behaviour of scanf: any white space in
    > the format causes scanf to read up to the next non-space
    > character, leaving it in the stream. This happens with %d
    > anyway, which is why "%d" and " %d" should be the same. If
    > scanf is not doing what it should then you are in a strange
    > situation and it is hard to give practical advice.


    I rarely use scanf, but wouldn't " %d" REQUIRE a space before the
    value, while "%d" will absorb all spaces before the value?

    --
    Merry Christmas, Happy Hanukah, Happy New Year
    Joyeux Noel, Bonne Annee, Frohe Weihnachten
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
     
    CBFalconer, Dec 29, 2008
    #5
  6. Tagore

    Eric Sosman Guest

    CBFalconer wrote:
    > Ben Bacarisse wrote:
    > ... snip ...
    >> I don't know what you mean by "clearing" but you should be able
    >> to rely on the specified behaviour of scanf: any white space in
    >> the format causes scanf to read up to the next non-space
    >> character, leaving it in the stream. This happens with %d
    >> anyway, which is why "%d" and " %d" should be the same. If
    >> scanf is not doing what it should then you are in a strange
    >> situation and it is hard to give practical advice.

    >
    > I rarely use scanf, but wouldn't " %d" REQUIRE a space before the
    > value, while "%d" will absorb all spaces before the value?


    No, the " " will match any number of white space input
    characters, including zero. 7.19.6.2p5:

    A directive composed of white-space character(s) is
    executed by reading input up to the first non-white-space
    character (which remains unread), or until no more
    characters can be read.

    If the very first character is non-white, the " " directive
    is finished and the non-white character remains unread, ready
    to be chewed over by the "%d".

    In short, " %d" should behave identically to "%d", and I
    am unable to explain the O.P.'s report in terms of the C Standard.
    Either he's mis-reported, or his C library has a bug.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 29, 2008
    #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,913
    Bryan Bullard
    Jul 11, 2003
  2. hugo27
    Replies:
    5
    Views:
    638
    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. barry

    fflush(stdin), scanf and a space

    barry, Jun 7, 2010, in forum: C Programming
    Replies:
    5
    Views:
    6,127
    Eric Sosman
    Jun 9, 2010
  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