Flushing stdin after every input - which approach is not buggy?

Posted by Aseem Bansal on Stack Overflow See other posts from Stack Overflow or by Aseem Bansal
Published on 2013-06-22T07:32:36Z Indexed on 2013/07/03 5:05 UTC
Read the original article Hit count: 224

Filed under:

While using scanf() in C there is always the problem of extra input lying in the input buffer. So I was looking for a function that I call after every scanf call to remedy this problem. I used this, this, this and this to get these answers

//First approach
scanf("%*[^\n]\n");

//2ndapproach
scanf("%*[^\n]%*c");

//3rd approach
int c;
while((c = getchar()) != '\n' && c != EOF)
        /* discard */ ;

All three are working as far as I could find by hit-and-trial and going by the references. But before using any of these in all of my codes I wanted to know whether any of these have any bugs?

© Stack Overflow or respective owner

Related posts about c