0

I have a problem with scanf in while loop. Made a simple piece of code for illustration:

#include <stdio.h>

int main() {
    int menu = 0;
    while (menu != -1) {
        if (!scanf("%d", &menu)) printf("n/a\n");
    }
    return 0;
}

If I enter an improper data like char in stdio, scanf doesn't stop the cycle for input and program just typing n/a over and over. With integers, it works properly: awaiting for input in every iteration. Can someone explain this behavior?

  • [What does the scanf function return?](https://stackoverflow.com/questions/10469643/what-does-the-scanf-function-return). And also @kaylum's comment might help too – AhmetK Feb 14 '22 at 05:08

0 Answers0