Another method to clear the input buffer(stdin
) would be to use
scanf("%*[^\n]%*1[\n]");
Here,%*[^\n]
instructs scanf
to scan everything until a new-line character(\n
) is found and then discard it.The %1*[\n]
tells scanf
to scan 1 character including a \n
character and discard it also.