I'm not sure if I properly understand how flushing works in C. I just can't get it to work as described in multiple manuals and reference books. Here's an example with comments:
#include <stdio.h>
int main(void) {
int x;
char ch;
printf("Prompt: ");
scanf("%d", &x); /* I key in 67 and press Enter. At this very moment,
the input buffer should contain the ASCII codes
for the numbers 6 and 7 as well as the ASCII code
for the newline character, which is 10. The scanf
function is going to find the ASCII codes for 6
and 7, convert them to the integer 67, assign it
to the variable x and remove them from the
buffer. At this point, what still remains in the
buffer is the newline character. */
fflush(stdin); /* I want to do some more input. So, I flush the
buffer to remove anything that still might be
there, but it doesn't really look like fflush is
doing that. */
printf("Prompt: "); /* I'm not going to be able to get my hands on
the following line of code because fflush is
not doing its job properly. The remaining
newline character is going to be read into the
variable ch automatically, thus not allowing
me to enter anything from the keyboard. */
scanf("%c", &ch);
printf("x: %d, ch: %d\n", x, ch);
/*
OUTPUT:
Prompt: 67
Prompt: x: 67, ch: 10
*/
return 0;
}