Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Excuse me, in C language, why doesn't gets stop when it encounters spaces, and why does scanf stop when it encounters spaces? Please enlighten me! There must be heavy thanks!
Excuse me, in C language, why doesn't gets stop when it encounters spaces, and why does scanf stop when it encounters spaces? Please enlighten me! There must be heavy thanks!
When typing on the keyboard, the space means null, that is to say, the value before the space is assigned to the variable at the end of input.

123 space Because the first one received is of character type, only' 1' 23 can be received, which is still in the buffer. At this point, the I variable only needs to receive the plastic data in 10 ... so 23 is assigned to i 23, followed by a space, and it stops.

After that, S receives the string 456 from the buffer, followed by the space reception stop, so the final answer is 1, 23,456.

If the third scanf is changed to scanf ("%[\ n] ",s); The answer should be number three (there is no comma between 456 and 789).

% [\n] receives all characters from character to \n character. Initially, scanf did not accept spaces. When a space is encountered, it will be treated as null to stop accepting the following characters, but the space will be received after the control input. In fact, it can also be received by the gets function, and gets also receives spaces.