Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 5
Breakdown of scanf("%*[^\n]%*c"):
%*[^\n] scans everything until a \n, but doesn't scan in the \n. The asterisk(*) tells it to discard whatever was scanned.
%*c scans a single character, which will be the \n left over by %*[^\n] in this case. The asterisk instructs scanf to discard the scanned character.
Both %[ and %c are format specifiers. You can see what they do here. The asterisks in both the specifiers tell scanf, not to store the data read by these format specifiers.
It will clear a single line of the stdin (Standard Input Stream) up to and including the newline character. In your case, the line with invalid input gets cleared from the stdin.
It is better to use
scanf("%*[^\n]");
scanf("%*c");
to clear the stdin.