This is (one of the) reasons why one should never use *scanf
: it's ridiculously difficult to get it to handle malformed input robustly.
The correct way to parse something like this is: use fgets
(or getline
if you have it) to read an entire line, manually check for and skip over the leading "s "
, then use strtod
to parse the number, then check whether strtod
set *endp
to point to a newline.
If the syntax is even a little more complicated than what you have it may be time to reach for lex
and yacc
.