What's so bad about scanf anyway???

Henry Spencer henry at zoo.toronto.edu
Wed Nov 14 06:32:19 AEST 1990


In article <16582 at netcom.UUCP> avery at netcom.UUCP (Avery Colter) writes:
>In the self-teaching course I have here, scanf is the most often used
>input function. I don't see gets used much at all.

It is not surprising that an introductory course will focus on doing things
the easy way rather than the better but more complex way, for the sake of
not confusing beginners.

As others have discussed at length, the problem with scanf is a poor and
inflexible design that gives you little control over the situation when
unexpected input is encountered.  Pulling in a line with fgets (not gets!)
and then picking it apart with sscanf makes clean error recovery much
easier.

>I didn't see puts used for printing strings to screen much either.
>printf was the function of choice.

People frequently draw this analogy, but it is false and misleading.
Printf works very well for output because its inputs are C data, very
tightly constrained by the language and the machine, and the free-form
version is what it is *generating*.  The situation is not symmetrical;
scanf is faced with a very different and much harder problem.
-- 
"I don't *want* to be normal!"         | Henry Spencer at U of Toronto Zoology
"Not to worry."                        |  henry at zoo.toronto.edu   utzoo!henry



More information about the Comp.lang.c mailing list