printf vs fprintf

Karl Heuer karl at haddock.ISC.COM
Tue May 24 09:13:09 AEST 1988


In article <200 at proxftl.UUCP> jesse at proxftl.UUCP (Jesse Perry) implores people
not to use scanf().  To avoid misunderstandings, I would rephrase it thus:

The scanf() family is for *free-format* input -- non-significant whitespace,
including newline, tends to be ignored.  If you need line-oriented (e.g.
prompted) input, you probably want to use something else.

>Using (f)gets() followed by sscanf() is much more reliable, since format
>errors can be detected and reported immediately.

Let me add that fgets() is better than gets() in that it does bounds-checking
(but even so, programs seldom do anything reasonable when it is detected).
Also, when using fgets/sscanf it's easy to overlook the error of having *too
much* data on one input line -- one way to catch this is to put " %c" at the
end of the format (and an appropriate dummy variable in the arglist), and
expect the return value to be one *less* than the number of variables passed.

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list