-1
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char text[256];
    
    while (1) {
        puts("Text?");
        fgets(text, sizeof(text), stdin);
        fflush(stdout); // should you flush everytime?
    }
    
    return 0;
}

For example, if you were to create user input and print in an while loop that doesn't break, should you flush? Is it also necessary to flush stdin?

If also you would fputs in stderr, would you also have to flush stderr in this scenario?

Jack Murrow
  • 350
  • 2
  • 7
  • What's the point of flushing after the `fgets`? It would make more sense to flush after the `puts` and before the `fgets`, to make sure the prompt is displayed before waiting for user input. But if you are actually interacting with the user on a terminal, then `stdout` is likely to be line buffered anyway, and `puts` prints a newline, so the flush is redundant. – Nate Eldredge Oct 04 '20 at 06:53
  • Someone will probably give a more detailed explanation, but in general no. With some exceptions, I never manually flush `stdin` or `stdout`. – Locke Oct 04 '20 at 06:54
  • @NateEldredge If lets say you use `printf` instead of `puts`, would it still be the same? – Jack Murrow Oct 04 '20 at 06:55
  • [`fflush(stdin)` is undefined behavior](https://stackoverflow.com/questions/2979209/using-fflushstdin) so never do that, period. – Nate Eldredge Oct 04 '20 at 06:55
  • If your prompt didn't have a newline then yes, you should `fflush(stdout)` (before `fgets`, not after) if you want the user to see the prompt before they have to type. But most programs that involve significant user interaction will be using something fancier than stdio anyhow (e.g. curses) so the issue rarely arises in practice. – Nate Eldredge Oct 04 '20 at 06:57
  • @NateEldredge This also implies to `stderr`, correct? – Jack Murrow Oct 04 '20 at 06:57
  • @JackMurrow: For maximum portability, yes, see https://stackoverflow.com/questions/3723795/is-stdout-line-buffered-unbuffered-or-indeterminate-by-default, but on many common systems `stderr` is unbuffered by default so there is no need to `fflush` it. – Nate Eldredge Oct 04 '20 at 06:59
  • @NateEldredge How about for C++? – Jack Murrow Oct 04 '20 at 07:00
  • I do not know as much about C++. You probably should have asked about C and C++ in two separate questions as they are two separate languages. However, for things like stdio, AFAIK C++ generally conforms to C (this is not necessarily true of other parts of the language). – Nate Eldredge Oct 04 '20 at 07:00
  • @Jack, For C++, you have `tie()` set up by default on `cout` and `cin` to flush `stdout` right before `stdin` is used. – chris Oct 04 '20 at 07:08
  • @chris Thank you I'll make a separate question for C++ – Jack Murrow Oct 04 '20 at 07:15

1 Answers1

0

You need to flush only when there are pending bytes in the buffer, that is, if there is not a newline inside text. Anyway, what you should flush is stdin (not stdout):

    fgets(text, sizeof(text), stdin);
    // fflush(stdout); // should you flush everytime? --> NOPS
    if (strchr(text, '\n') == NULL)
    {
        int c;
        // Flush stdin
        while (((c = fgetc(stdin)) != '\n') && (c != EOF));
    }
David Ranieri
  • 36,077
  • 6
  • 44
  • 85