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 109 views
109 views asked Aug 8, 2024 by avibootz
2 answers 226 views
226 views asked Feb 9, 2021 by avibootz
1 answer 197 views
1 answer 201 views
1 answer 234 views
234 views asked May 27, 2015 by avibootz
...