1

I used to use rewind() function to clear the input buffer in the Visual Studio 2019, But I can not use rewind() function in the GCC compiler. (Simple Example >>)

scanf("%s",temp);
rewind(stdin) /* Are there any ways to clear the input buffer?*/
  1. Which function should I use to clear the input buffer in the GCC compiler?

  2. Are there any good ways to clear the input buffer without using some functions?

kkang1202
  • 11
  • 2
  • 5
    When you say "clear the input buffer" do you mean extract and ignore all characters until newline? Then just read and ignore all characters until newline (or `EOF`). – Some programmer dude Apr 15 '21 at 05:56
  • 2
    Tip: Don't use `scanf()`. It's pretty terrible at everything it purports to do. Just use `fgets()` and do your own parsing. – tadman Apr 15 '21 at 06:26
  • 1
    scanf isn't that bad, when used in the right way – paladin Apr 15 '21 at 06:27
  • @Someprogrammerdude Thanks for your comment. I will try it in my code:) – kkang1202 Apr 15 '21 at 07:35
  • @Yunnosch Thanks for your comment. The answer is helpful for me:) – kkang1202 Apr 15 '21 at 07:38
  • 1
    @tadman Thanks for your comment. I will try to use fgets() instead of scanf(). :) – kkang1202 Apr 15 '21 at 07:40
  • @paladin Thanks for your comment. I will check it! :) – kkang1202 Apr 15 '21 at 07:41
  • 1
    @paladin True. And how easy it is to use it right can be seen by how short the descriptions are. E.g. the one under this (misleading, because it actually helps with using scanf) http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html It is practically a book ... – Yunnosch Apr 15 '21 at 09:05
  • 1
    @Yunnosch You'll get this kind of infinity loop every time when you never read the API specification of scanf and co.. _"On success, the function returns the number of items of the argument list successfully read. If a reading error happens or the end-of-file is reached while reading, the proper indicator is set **(feof or ferror)** and, if either happens before any data could be successfully read, **EOF** is returned."_ Your linked tutorial doesn't even mention **feof or ferror**, which illustrates how bad that tutorial is. – paladin Apr 15 '21 at 12:56

0 Answers0