How to clear stdin buffer in C

1 Answer

0 votes
#include <stdio.h>

int main(void)
{
    char ch;
    
    do 
    {
        printf("your code is here\n");
        printf("Enter a character (Q/q to quit): ");
        fseek(stdin, 0, SEEK_END);
        ch = getc(stdin);
    } while (ch != 'Q' && ch != 'q');
        
   return 0;
}

/*
 
run:

your code is here
Enter a character (Q/q to quit): a
your code is here
Enter a character (Q/q to quit): b
your code is here
Enter a character (Q/q to quit): ooo
your code is here
Enter a character (Q/q to quit): w
your code is here
Enter a character (Q/q to quit): q

*/



answered Sep 14, 2014 by avibootz

Related questions

1 answer 107 views
107 views asked Aug 8, 2024 by avibootz
2 answers 214 views
214 views asked Feb 9, 2021 by avibootz
1 answer 191 views
1 answer 196 views
1 answer 228 views
228 views asked May 27, 2015 by avibootz
1 answer 129 views
...