0

I can't seem to understand the concept of fflush() function in C. Could someone explain it in simpler terms cause I cannot seem to grasp it and what it does in this code:

int main() {
    loadContactList();
    while (1) {
        printf("\n");
        printMenu();

        int choice;

        scanf(" %d", &choice);
        fflush(stdin);
        printf("\n");

        if (choice == 1) {
           // addContact();
        } else if (choice == 2) {

        } else if (choice == 3) {

        } else if (choice == 4) {
            query();
        } else if (choice == 5) {
            while (1) {
                printf("choose the sorting mode:\n \n");
                printf("1. Sort by last name, first name then number\n");
                printf("2. Sort by date\n");
                printf("Enter -1 to return to the main menu\n");

                int x;

                scanf("%d", &x);

                if (x == 1) {
                    sortByLFN();
                    printContactList();
                    break;
                } else if (x == 2) {
                    sortByDate();
                    printContactList();
                    break;
                } else if (x == -1) {
                    break;
                }
            }
        } else if (choice == 6) {
            //saveContact();
        } else if (choice == 7) {
            quitPhoneBook();
        } else {
            printf("You entered an invalid option \n");
        }    
    }
    return 0;
}

the code is supposed to be for a phone book program and we where told to use fflush but it wasn't explained in class.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Most people here will probably recommend to not flush input and instead use indentation and less spacy white space. – Yunnosch Dec 23 '18 at 18:54
  • 1
    The proposed duplicate is indirect. Flushing input is undefined behaviour (see the upvoted but not accepted answer in the proposed dupe). This indirectly gives the answer to "What to use flush for in this code? Not at all." What to do instead can also be found in that question. This is tricky if you were told to use flush on input by a teacher.... – Yunnosch Dec 23 '18 at 18:57
  • Note that flushing output makes sense though. I am used to achieving the desired output by appropriate use of newlines (which in most environments allows enough output control), but flushing is another method and in certain cases necessary (I believe). – Yunnosch Dec 23 '18 at 18:58
  • 1
    Unrelated to your question, but I recommend that you learn about the `switch` statement. – Some programmer dude Dec 23 '18 at 18:58
  • The proposed duplicate (assuming that you accept it or at least find it helpful) is by the way found among the "related" questions on the right of the page. For future questions, please make sure to at least read the questions which the (actually not very intelligent) system considers related. Referring to them and explaining for the most promising, why your question is NOT a duplicate of them gives a very good impression; it shows "research effort", which is one of the three attributes expected of question for being upvoted. Also appreciated is taking the [tour]; you are supposed to ... – Yunnosch Dec 23 '18 at 19:05
  • The reason to not flush input is that the call does not make any sense when it comes to POSIX or Linux or any other similar semantics. It doesn't do anything specified if you're reading from a terminal... – Antti Haapala -- Слава Україні Dec 23 '18 at 19:15
  • 1
    Have you read the [documentation of `fflush`](https://en.cppreference.com/w/c/io/fflush) ? – Basile Starynkevitch Dec 23 '18 at 19:48

1 Answers1

5

Flushing an output stream (such as stdout) causes any buffered data to be "flushed" to the output. For example, flushing stdout is often used to ensure that the output becomes visible even if it is not followed by a newline, since stdout may be line-buffered.

Flushing an input stream (such as stdin) is undefined behaviour in standard C, and should not be used. Some implementations do define it as a non-standard extension to clear any unread input, but I would strongly recommend against exploiting this (in particular as a workaround for improper use of scanf). The code in the question falls into this category.

Arkku
  • 41,011
  • 10
  • 62
  • 84