Problems with scanf()

Trent Tobler ttobler at unislc.uucp
Sat Oct 20 10:16:37 AEST 1990


[many comments on the evils of scanf]

People, scanf is not BAD, only people who use it as their primary input
function.  Scanf is a very powerful pattern matching function and can
save a lot of code in parsers and tokenizers.  

I think the main reason everyone is anti-scanf is that in many languages,
there is a primary input function (readln, input, etc.), and scanf is
often assigned this purpose by many C programming classes/tutorials.
This is the reason that many of the problems of scanf are encountered.
I believe that scanf should be taught only after a strong knowlegde in C
is attained.  The gets function should be used as the beginning primary
input function.  Later, when files are learned, this should switch to fgets,
to avoid the problem of overflow allocated string space.  Scanf should not
ever be considered a primary input function.  Use it to parse, and match
patterns from the input stream.

I think most people who abhore scanf have been told by other people that scanf
is EVIL.  More than likely, it was after a catastrophic failure from thier use
of it as a primary input function.

Improper use can cause infinite loops, lockups, and core dumps.
But, on the other side, scanf is a very powerful tool .  It can reduce time
to develop a program, reduce code size, and even make a program more
maintainable.



         Trent Tobler.



More information about the Comp.lang.c mailing list