Talking about scanf (was Re: What's so bad about scanf anyway???)

Martin Weitzel martin at mwtech.UUCP
Thu Nov 15 06:04:07 AEST 1990


In article <VXogs2w163w at cybrspc> roy%cybrspc at 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 at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.lang.c mailing list