%[^\n] does not search for lines separated by spaces. It reads the entire line before (but not including) a new line.
If your string contains more than 9 characters, this causes undefined behavior by overflowing the buffer.
Buffer overflows can be prevented by writing %9[^\n] , but then you have a new problem: on the longer line, %*c will drop the 10th character, and the next scan will continue reading from the same line.
Another complicating factor is that if your file contains an empty line, then %[ considers it inappropriate. This means that scanf stops, so it does not go to %*c processing. In this case, a new line is not consumed, and the output buffer is not written at all.
Your code also goes into an infinite loop, because you never exit while(1) .
This code shows the correct use of ^[ :
while (1) { str[0] = '\0'; // In case of matching failure scanf("%9[^\n]", str); // read as much of the line as we can scanf("%*[^\n]"); // discard the rest of the line if ( getchar() == EOF ) // discard the newline break; // exit loop when we finished the input printf("%s\n", str); }
If you really want to read text separated by spaces, you can use %9s instead of %9[^\n] in my example above. This makes a new difference: %s skips an empty line.
If it is OK, then it is OK. If you don't want to skip blank lines, you can use my code above, but add to the end:
char *p = strchr(str, ' '); if ( p ) *p = '\0';
Reliable string input is difficult in C!