First of all, trailing s not part of the %[ format specifier, so remove it and talk about %[^\n] .
Now, what %[^\n] tells scanf to do is scan everything down to the newline character ( '\n' ) or EOF , whichever comes first, and stores it in the corresponding argument, in this case arr .
And here's the catch: %[^\n] fails if the first character read is \n .
βWait,β you say. "I did not go it alone. So, why didn't it work?" True. You do not. But remember the Enter that you pressed for the previous line? It turns out that the previous call to scanf captures everything until \n leaves \n there and returns. So, in the next iteration of the loop, scanf sees this \n character remaining the previous call to scanf , fails and returns 0.
As for space, this is a whitespace character. And the whitespace in scanf tells it to scan and discard all whitespace to the first character without spaces. This way it removes \n (since it is a space character), and scanf will wait for input.
source share