What's so bad about scanf anyway???

Roy M. Silvernail roy%cybrspc at cs.umn.edu
Sun Nov 11 18:49:54 AEST 1990


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.

The problem with scanf() is that it can behave unpredictably when you
give it badly formatted input. It's better, IMHO, to gets() a whole
line, check its validity and _then_ sscanf() it into the target
variables. (no need for strtol() or similar, since sscanf() looks at the
validated string just as scanf() would have looked at the original
input) It just makes things more bullet-resistant.

--
Roy M. Silvernail |+|  roy%cybrspc at cs.umn.edu  |+| #define opinions ALL_MINE;
main(){float x=1;x=x/50;printf("It's only $%.2f, but it's my $%.2f!\n",x,x);}
"This is cyberspace." -- Peter da Silva  :--:  "...and I like it here!" -- me



More information about the Comp.lang.c mailing list