【C】First enter an integer, then enter a string with spaces.

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 \nthe other characters before in the buffer one by one, and %the later ones *means discarding the read characters and \nstopping reading when the character is encountered. At this point, there is still one \nleft 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 \ncharacter, \nreading and discarding all the characters before it is equivalent to clearing the input buffer.

Guess you like

Origin blog.csdn.net/m0_50939789/article/details/129375343