In defense of scanf() (Re: Re^2: scanf(..))

Blair P. Houghton bph at buengc.BU.EDU
Thu Jun 15 03:15:42 AEST 1989


In article <4529 at ficc.uu.net> peter at ficc.uu.net (Peter da Silva) writes:
>scs at adam.pika.mit.edu (Steve Summit) writes:
>>It is only a miserable problem when scanf
>>is being used for interactive user input, which is what everybody
>>uses it for.

"Eeep", and "yoicks!"

>Anyone using scanf directly for interactive input... or for any input at
>all... should have their head examined.
>
>The only really safe way to use scanf() without freaking out the casual
>user of your code is to do something like this:
>
>	fgets(buffer, sizeof buffer, stdin);
>	sscanf(buffer, fmt, args...);

I'll go along with the "don't use it for interactive input" idea, but
not the "for any input at all"...

When filtering tabular data from files, or dealing in a situation where
a precise syntax is necessary, the `fgets(..); sscanf(..)' doublet just
adds uncertainty and complexity to a simple problem to which scanf is
suited ideally.  If there is an error reading something in that case,
you usually force a barf.  It's irrelevant whether the input gets
discarded.

The point is, don't reject scanf() just because it's unsuited to a problem
you aren't solving.

				--Blair
				  "We pay csh for used car's..."



More information about the Comp.lang.c mailing list