2

Is there any other method to clear the input buffer in c withut using

  fflush();

or

  while(getchar()!='\n');

Because i have read it everywhere and cant find any other way to do it.

rahul tyagi
  • 613
  • 1
  • 6
  • 20
  • How about `while (scanf("%c",&c) == 1)`? – barak manos Dec 03 '14 at 13:46
  • @barakmanos sorry sir, but din't you mean `while (scanf("%c",&c) == 1);`? – Sourav Ghosh Dec 03 '14 at 13:47
  • `rewind(stdin);` and `fseek(stdin,0,SEEK_END);` might work although i've not tested it... – Spikatrix Dec 03 '14 at 13:47
  • Yeah, well, you'd be better off with the `;`... – barak manos Dec 03 '14 at 13:48
  • 2
    What is the use-case where you need something other than this? – Gopi Dec 03 '14 at 13:53
  • what is worng with fflush(stdin)? – Jonatan Goebel Dec 03 '14 at 14:21
  • 1
    @JonatanGoebel , [Using `fflush` on stdin is Undefined Behaviour](http://stackoverflow.com/questions/2979209/using-fflushstdin) – Spikatrix Dec 03 '14 at 14:28
  • **No** `stdin = 0`, **man fflush: "If the stream argument is NULL, fflush() flushes all open output streams."** So `fflush(stdin)` - flushes all open output streams and has nothing to do with the input buffer. – David C. Rankin Dec 03 '14 at 17:48
  • Fflush() is not recommended in the documentation nd that while method sometimes results in a loop if not placed at the right place . i hope it helps – rahul tyagi Dec 03 '14 at 18:09
  • How about `__fpurge(stdin)` ?, defined in `#include `, I say [`__fpurge()`](http://linux.die.net/man/3/fpurge) and not `fpurge()` since the former is present in `glibc`, the latter isn't. Both are non-standard functions. Its worked quite well for me so far, however I'm not aware of its drawback, can't vouch for it. – Saurabh Meshram Dec 12 '14 at 11:48

3 Answers3

2

The best solution is to not depend on the input buffer's state so much.

Read input as whole lines, using fgets(), then parse those. Don't use e.g. scanf() to read individual values, since it interacts with the buffer in annoying ways.

unwind
  • 364,555
  • 61
  • 449
  • 578
  • But i m talking about the case eg when using getchar( ) some "/n " or other characters which have not been read interferes with the functioning .using fgets() or getline() is another issue thanks for the reply though . – rahul tyagi Dec 03 '14 at 18:23
2

Using fgets() as suggester @unwind best approach.

To flush to the end of the line.

void FlushStdin(void) {
  int ch;
  while(((ch = getchar()) !='\n') && (ch != EOF));
}

If stdin is all ready flushed to the end-of-line, calling FlushStdin() or other posted scanf(), fgetc() solutions, will flush to the end of the next line.

Note scanf("%*[^\n]%*1[\n]"); does not work if the next char is '\n'.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 113,725
  • 11
  • 107
  • 213
1

Another method to clear the input buffer(stdin) would be to use

scanf("%*[^\n]%*1[\n]");

Here,%*[^\n] instructs scanf to scan everything until a new-line character(\n) is found and then discard it.The %1*[\n] tells scanf to scan 1 character including a \n character and discard it also.

Spikatrix
  • 19,378
  • 7
  • 34
  • 77
  • `flushstdin.c:9:5: warning: unknown conversion type character ‘*’ in format [-Wformat=]` – David C. Rankin Dec 03 '14 at 16:28
  • "Here, `%*[^\n]` instructs `scanf` to scan everything until a new-line character(\n) is found and then discard it." is not true if the first `char` is `'\n'`. `'\n'` remains in `stdin`. – chux - Reinstate Monica Dec 03 '14 at 20:31
  • @DavidC.Rankin , Thanks. Fixed it – Spikatrix Dec 12 '14 at 11:07
  • Using `%*c` instead of `%*1[\n]` does not solve the problem when input is only `'\n'` as that 2nd part of the format specifier is not reached. Instead code code code use 2 independent `scanf()` calls: `scanf("%*[^\n]"); scanf("%*1[\n]");` or `scanf("%*[^\n]"); getchar();`. – chux - Reinstate Monica Dec 12 '14 at 12:33
  • @chux ,Isn't the `%` at the end of `scanf("%*[^\n]%");` a typo? Anyway,Thanks for making me understand! – Spikatrix Dec 12 '14 at 12:37