sscanf("8", "%o" &x); (was: How to validate input?)
Zev Sero
zvs at bby.oz.au
Wed Dec 5 13:15:19 AEST 1990
Stan = browns at iccgcc.decnet.ab.com (Stan Brown)
Chris = chris at mimsy.umd.edu (Chris Torek)
It is clear that
strtoul("8", (char **)0, 8)
succeeds and returns 0. The question is whether the *scanf conversion
function can be regarded as having `succeeded'. Stan thinks that it
should, quoting at length from the Standard, and Chris thinks that it
should not.
I haven't got the Standard, but Plauger & Brodie is supposed to be the
next best thing, and I quote from page 113 (Scan Functions):
Except for the conversion specifier n (which consumes no input),
each conversion specification determines a pattern that one or more
of the next characters in the input must match.
It seems to me that this is the important bit. For a conversion to
succeed, not only must the called function succeed, but it must match
one or more characters. In this case, strtoul matches zero characters,
so the conversion fails, x keeps whatever value it had before, and
strtoul returns 0.
--
Zev Sero - zvs at bby.oz.au
`...And the way to do something about it is to do something about it.'
- Joan Kirner (29/11/90)
More information about the Comp.lang.c
mailing list