0

I'm using eclipse in Windows, and minGW. I write the easy code like this.

#include <stdio.h>
int main()
{
    char c;
    printf("input: ");
    scanf("%c",&c);

    return 0;
}

Buffer error is rised, and I search the stackoverflow. It told me to use fflush(). And it works. But, I must write fflush() after each printf(). I don't wanna do this. I want to solve this problem in one statement.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • You could change `scanf("%c", &c);` to `scanf(" %c%*c", &c);`. – isrnick Apr 16 '21 at 02:06
  • Also don't use `fflush()` with `stdin`: https://stackoverflow.com/questions/2979209/using-fflushstdin. If you want to clean everything that is left in `stdin` you can declare `int ch;` and use `while((ch = getchar()) != '\n' && ch != EOF);` to clean it instead. And it should be put after the `scanf()`, not after `printf()`. – isrnick Apr 16 '21 at 02:08
  • The shown code should not throw a buffer error. Are you sure you posted your real [example]? -- Anyway, Eclipse as most other IDEs does not provide a real terminal. And all output is commonly buffered some way, depending on the kind of output target. Eclipse seems to use a pipe, which might be handled as a file that is probably block buffered. If you use a real terminal, this is commonly line buffered, which means that output needs a `'\n'` before it is shown. I'm afraid you need that `fflush(stdout)` or set the buffer mode. – the busybee Apr 16 '21 at 09:27
  • Try [setvbuf()](https://stackoverflow.com/a/7876756/1983398). – ssbssa Apr 16 '21 at 12:57

0 Answers0