Thread: Alternative for fflush(stdin)

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    82

    Alternative for fflush(stdin)

    Hey guys,

    As I was searching for an alternative for
    Code:
    fflush(stdin)
    , I came across
    Code:
    scanf(%*[^\n])
    , does this function have any disadvantages? And what does * do?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You've typed the scanf() call incorrectly. It would be
    Code:
    scanf("%*[^\n]");
    (note the double-quote characters to delimit the format string).

    In any event, fflush(stdin) has undefined behaviour, so should never be employed. Not ever. Any textbook that tells you you can should be summarily burnt.

    A distinct advantage of that scanf() call is that it has well defined behaviour. It will discard all characters up to a newline. The newline character will remain, and be encountered by the next statement that reads from stdin.

    As to disadvantages..... Firstly, it is insufficient to cope with a user who manages to type ahead multiple lines of text, as it will only discard the first line.

    The other big disadvantage is that mixing styles of input on an input stream is a bad idea. If all of your input is going to be read using scanf(), and you trust your users not to type ahead multiple lines (big assumption) the approach will suffice. If your code is going to use character oriented input (e.g. getc() or getchar()) or line oriented input (e.g. fgets()) after that scanf() call, the behaviour will be a little counter-intuitive for the user.

    The * simply causes the next formatting ([\n]) to read input, but not write to a variable.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by grumpy View Post
    You've typed the scanf() call incorrectly. It would be
    Code:
    scanf("%*[^\n]");
    (note the double-quote characters to delimit the format string).
    Oops, I had forgotten that. Any other way to flush stdin? A better way perhaps?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,652
    Normally, I would suggest using fgets() for everything, and using a suitably sized buffer (BUFSIZ is good).

    fgets() will grab everything upto a \n, and you're then free to use/ignore the buffer as necessary.

    The whole problem of 'flushing' the input buffer pretty much goes away if you do this, since you always know what state the input stream is in.
    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.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Same FAQ that explains FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    contains a link to another FAQ that explains how to do it correctly FAQ > Flush the input buffer - Cprogramming.com
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. alternative for fflush(stdin)
    By Raison in forum C Programming
    Replies: 11
    Last Post: 04-13-2011, 04:58 AM
  2. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  3. fflush(stdin);?!?!
    By AmeenR in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2003, 09:46 AM
  4. fflush(stdin)
    By RyeDunn in forum C Programming
    Replies: 24
    Last Post: 07-18-2002, 09:07 PM
  5. ascii 10 in the stdin after fflush(stdin)??
    By Kev2 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-03-2002, 03:53 PM

Tags for this Thread