Ben Bacarisse <> writes:
[snip]
> The problem with strtok has been pointed out, but you can continue to
> use it because you don't really need it here. You expect only one pair
> or maybe a lone number and you can parse that using sscanf:
>
> sscanf(token, "%d-%d", &low, &high)
>
> will return 1 for lone numbers, 2 for a pair like 1-3 and anything else
> is an error and needs to be reported.
[...]
Keep in mind that sscanf's behavior is undefined if you scan a number
outside the range of the specified type. For example,
if INT_MAX==32767, then this:
sscanf("40000-50000", "%d-%d", &low, &high);
has undefined behavior. Which is a great pity; it makes the *scanf()
functions very difficult to use safely for numeric input.
With a bit of extra work, you can use the strto*() functions instead;
they're sane enough to tell you if the value is out of range (by
returning an extreme value and setting errno to ERANGE).
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"