Let's look at your scanf
format specifier "%d-%d"
in detail:
%d
skip whitespace if necessary, then read an integer
-
match a literal '-'
character
%d
skip whitespace if necessary, then read an integer
So the inputs 5-3
and 5- 3
both work just fine. But when the input is 5 -3
(or anything else with a space before the -
) the parse fails, because scanf
does not immediately see the -
it expects.
If that's not what you expected, or not what you want, or if that doesn't make sense, or if that's not how you'd like scanf
to work, I'm afraid that's too bad: scanf
works the way it works.
How can you fix this? That depends in part on why you included the -
character in your format string in the first place.
- You can use
%d%d
or %d %d
, which will simply read two integers (separated by at least one whitespace character). If there's a -
character preceding the second integer, that integer will be read as negative.
- You can use
%d -%d
, which will skip (arbitrary) whitespace before it tries to match the -
character.
- You can use two separate
scanf
calls.
- If you do continue to use
scanf
, you really need to check its return value so that your program can detect the case that the expected inputs were not matched.
- Finally, you can use something other than
scanf
.
My recommendation to you depends on the ultimate purpose of this program.
- If it's just for learning, then minimize the amount of time you spend fussing with the way your program does input at all. For example, if you need to read an integer, use one
%d
. As long as you can get the numbers you need into your program (so that you can test the rest of your program), you're fine. If there are things you can type that cause scanf
to get confused, just don't worry about it, just don't type those things. Don't try to do anything fancy — that's not what scanf
is for.
- If this is a "real" program, that does have to accept arbitrary user input, or input with a specific syntax (like with that
-
in the right place), and if you need to deal gracefully with incorrect input, printing appropriate errors, and not reading the wrong values or getting confused — then run, do not walk, away from scanf
, and never use it again. It is effectively impossible to write a program that performs high-quality input using scanf
. Not even a C expert can do it. It's simply not worth it. You will spend five times as long, and get an inferior result, than if you simply abandoned scanf
and read your input a line at a time using fgets
or the like, then parsed the input line (perhaps even using sscanf
— but again, check its return value).
Addendum: It's true, all format specifiers — with three exceptions (%c
, %[…]
scan sets, and %n
) — skip whitespace before beginning their work. But format specifiers are things that begin with %
. Literal characters in the format string must match exactly, and there's no implicit whitespace skipping for them. If you want to skip whitespace at spots in the input other than before the %
format specifiers that do it, you can include a literal whitespace character (usually a single space) in your format string.