0

As we know that arrow keys produce two outputs i.e. 224 and (72 or 80 or 75 or 77).

CODE 1:-

char ch,ch1;

ch=getch();

ch1=getch();

printf("%c \n %c",ch,ch1);

When in the above case, I input an arrow key then 224 is stored in ch and the corresponding output is stored in ch1.

CODE 2:-

char ch,ch1;

ch=getch();

fflush(stdin);

ch1=getch();

printf("%c\n%c",ch,ch1);

Same thing happens in the code 2 also.
So I want to know that why fflush(stdin) is not flushing the corresponding output to 224.

kevin gomes
  • 1,775
  • 5
  • 22
  • 30
  • actually, flushing the input buffer does not do a whole lot. People often think it is some cure to bad data but it isn't. fflush is from output buffers, it forces all data in the buffer to be written. The reason it does not do anything for the input buffer is because your program is the destination of data -- fflush does not know how to ask your program to consume data – Jayesh Bhoi Mar 19 '14 at 07:08
  • possible duplicate of [Using fflush(stdin)](http://stackoverflow.com/questions/2979209/using-fflushstdin) – Lundin Mar 19 '14 at 07:12

2 Answers2

1

I think you want fpurge. fflush is for output streams, fpurge is for input streams.

user3386109
  • 34,287
  • 7
  • 49
  • 68
  • There exists no function called fpurge in standard C. That's some Linux library function. – Lundin Mar 19 '14 at 07:13
  • FYI I'm not running Linux, and OP may or may not have access to `fpurge`. – user3386109 Mar 19 '14 at 07:15
  • @user3386109 : then how fflush(stdin) works for clearing inputs like `enter key` – kevin gomes Mar 19 '14 at 07:16
  • @kevingomes It doesn't. `fflush(stdin)` is _undefined behavior_ and the only thing its good for is to cause your program to potentially crash and burn. – Lundin Mar 19 '14 at 07:18
  • @Lundin : that's what I want to know. If it is undefined behaviour then how it works for inputs like `enter key`. And what Is the solution for my case? – kevin gomes Mar 19 '14 at 07:22
  • You don´t understand what Lundin says. Undef. behaviour => maybe it works now (today, on this computer...), but tomorrow/elsewhere not anymore. The solution is to read manually. And, unrelated, we don´t know what the arrow keys are producing, because that´s undefined too (as far as i know). – deviantfan Mar 19 '14 at 07:31
  • @kevingomes You question is already asked [here](http://stackoverflow.com/questions/2979209/using-fflushstdin) and undefined behavior is explained [here](http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). – Lundin Mar 19 '14 at 07:52
1

fflush(stdin) though works on some implementations, it's still undefined behavior. According to the standard fflush , fflush only works with output/update streams.

int fflush(FILE *ostream);
If stream points to an output stream or an update stream in which the most recent operation was not input, fflush() shall cause any unwritten data for that stream to be written to the file, [CX] [Option Start]  and the last data modification and last file status change timestamps of the underlying file shall be marked for update. [Option End]

Some Compilers have defined this feature of flushing of input streams , but If you have a compiler without this particular enhancement then You will spend days trying to figure out what's wrong

The solution for flushing stdin would be something like this

int c;
while ((c = getchar()) != '\n' && c != EOF);
Vivek Aditya
  • 1,145
  • 17
  • 46