What's so bad about scanf anyway???

Jay A. Konigsberg jak at sactoh0.SAC.CA.US
Mon Nov 12 05:21:39 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.
>
>And indeed, gets only seems of much advantage when you want to take in
>a whole line into one string.
>
IMHO gets() or getchar() is better for input because the programmer
has greater control over what is being input. Specifically, if the
programmer wants a float value and a character is input gets() won't
error on it, scanf() will. My argument goes mainly to bullet-proofing
programs.

>Otherwise, scanf can take individual numbers and put them directly into
>numerical variables. With gets, you'd have to first manually parse the
>line, and then use strtol to translate them into numbers.
>
Generally, I'll use scanf() when reading from a file that a program
has created. Then scanf() is superior.

>I didn't see puts used for printing strings to screen much either.
>printf was the function of choice.
>
puts()/fputs() is generally faster than printf() and should be used
when possible. However, I will confess that printf() is much more
commonly used. Perhaps its because printf() will handle all cases
and puts() will only handle the string only case.


-- 
-------------------------------------------------------------
Jay @ SAC-UNIX, Sacramento, Ca.   UUCP=...pacbell!sactoh0!jak
If something is worth doing, it's worth doing correctly.



More information about the Comp.lang.c mailing list