Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!usc!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!unido!mikros!mwtech!martin From: martin@mwtech.UUCP (Martin Weitzel) Newsgroups: comp.lang.c Subject: Talking about scanf (was Re: What's so bad about scanf anyway???) Message-ID: <960@mwtech.UUCP> Date: 14 Nov 90 19:04:07 GMT References: <16582@netcom.UUCP> Reply-To: martin@mwtech.UUCP (Martin Weitzel) Organization: MIKROS Systemware, Darmstadt/W-Germany Lines: 31 In article roy%cybrspc@cs.umn.edu (Roy M. Silvernail) writes: >[about reading 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. I agree completly, except for one point: Bullet resistant programs should never use gets() -- fgets() is the way to go! BTW: Recently there occured a problem, to which I couldn't find an immediate solution. Take the following program fragment: #define X 100 char word[X+1]; int z; scanf("%100[^:]:%d", word, &z); ^^^----------------------- I'd rather want X here; still better were `(sizeof word) - 1', so that I could ommit the #define for X completly What I want is to have only *one* place where I define the length of a word. In this example, if I change the #define of X, I must never forget to make a change at a second place, which may be far away from the first. What I could do is dynamically (at runtime) construct an appropriate format specification. But IMHO this more a work-around than an elegant solution to the problem. -- Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83