Implement code
int row, col;
scanf("%d %d", &col, &row);
scanf("%*c");
char input[100];
scanf("%[^\n]", input);
scanf("%*c");
explain
scanf("%*[^\n]"); scanf("%*c");
The first one scanf()
will read \n
the other characters before in the buffer one by one, and %
the later ones *
means discarding the read characters and \n
stopping reading when the character is encountered. At this point, there is still one \n
left in the buffer
The second one reads and discards scanf()
this \n
. The asterisk here scanf()
has the same effect as the asterisk in the first one. Since all input from the keyboard ends with a carriage return, and carriage return generates a \n
character, \n
reading and discarding all the characters before it is equivalent to clearing the input buffer.