"%[^\n]s"
tells to scanf
to "eat" all the character that aren't a newline and put them in the argument; this means that, after the first scanf
returns, it is leaving the newline that made it stop in the read buffer. The next scanf
finds it and immediately stops reading - after all, you told it to read up to the first newline!
A solution here can be to use "%[^\n]s\n"
, which "eats" even the newline that follows the string, or even "%[^\n]s "
, where the space is "magic", in that it tells scanf
to eat all the other whitespace it can find, which includes newlines (notice however that this is a bad idea if you want to be able to read an empty string in the next line).
Even simpler, you can simply use fgets(string1, sizeof(string1), stdin)
, which, unlike scanf
with %s
, is safe against buffer overflows (yes, you can make even %s
safe, but it's more work). Notice that fgets
puts even the trailing newline into your string.