0

There are a few questions floating around on stack overflow that, when synthesized, likely answer this question. However, I just wanted to make sure beyond any doubt that the following interpretation of a code segment is correct.

In "C Programming: A Modern Approach", the following code idiom involving a scanf() function and a getchar() function is occasionally seen. Consider the following example, taken from Page 392:

int main(void)
{
  char code;
  for (;;) {
    printf("Enter operation code: ");
    scanf(" %c", &code);
    while(getchar() != '\n')
      ;
.../*other stuff*/
  }
}

Here is my understanding of what is going on with the getchar() function embedded within a while loop.

When the user is prompted to enter a character, it is very possible that the user presses many buttons (e.g. abcdefg 12 3 , . f) into the command terminal.

In the absence of the getchar() line, scanf() (which is embedded within an infinite loop) would otherwise iteratively chug through that input buffer.

i.e. On the first pass, code would be assigned the character 'a' and the input buffer would have bcdefg 12 3 , . f remaining...on the second pass through the infinite for loop, code would be assigned character 'b' and cdefg 12 3 , . f would be remaining...etc.

However, by including the getchar() line, we are effectively draining the input buffer so that prior to advancing to the \* other stuff *\ section, the input buffer is completely empty.

Is this the correct idea?

If it is, then my final question is why does the getchar() line need to be written as != '\n'. Couldn't I have written the condition as being not equal to literally anything? e.g. != ',' or !='d' or != ' ' ...etc.

Is it just for readability (i.e. conforming to the quasi-convention of using the Enter button to submit information to the computer)?

Thanks~

S.C.
  • 231
  • 1
  • 8
  • 1
    Does this answer your question? [scanf() leaves the new line char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer) – paulsm4 Sep 07 '20 at 20:50
  • 1
    It clears the remains of the line from the `stdin`. It will work if the `scanf` was successful or not. The next `scanf` will scan the new line. – 0___________ Sep 07 '20 at 20:50
  • @paulsm4 I'm not seeing why that is relevant. Wouldn't the line `while(getchar() != ','` or anything else of that nature also "consume" the new line character?...and also, the `scanf()` function here makes use of the ` %c` trick...where there is white space before the `%c`...therefore, doesn't that already address this concern? – S.C. Sep 07 '20 at 20:57
  • 2
    @S.Cramer No, because any stuff left after the `,` would remain. In *normal* circumstances, there will be nothing left after a newline. However, your remark about the space before `%c` is very relevant, I think. – Adrian Mole Sep 07 '20 at 21:00
  • @AdrianMole ahhhh. Gotch ya. Thank you! – S.C. Sep 07 '20 at 21:02
  • 2
    The entire `while` loop can be avoided by doing `scanf(" %c%*[^\n]\n", &code);` instead. – dxiv Sep 07 '20 at 22:26
  • 1
    @dxiv various problems with that including `scanf()` not returning until a second line is entered. – chux - Reinstate Monica Sep 08 '20 at 03:41
  • 2
    "Is this the correct idea?" --> Yes but note that if `stdin` is closed, `while(getchar() != '\n') ;` never quits. Best to look for `EOF` also. – chux - Reinstate Monica Sep 08 '20 at 03:42
  • @chux-ReinstateMonica Or EOF is reached, which makes it one better than the posted `while` loop ;-) – dxiv Sep 08 '20 at 05:12

1 Answers1

1

Yes it is absolutely fine using getchar() for clearing input buffer. I would say it is best way for clearing input bufffer rather than using fflush , %*c or any other method.

'\n' simply means ENTER KEY (ascii value-10) So, the while loop will run until \n is encountered.

input buffer would contain:

abcdefg    12 3 , . f\n
MD DANISH
  • 36
  • 4
  • the function: `fflush()` is ONLY for output streams and `stdin` is an INPUT stream. Using `fflush()` on an input stream is specifically stated in the C standard to be Undefined Behavior – user3629249 Sep 08 '20 at 22:36