how has C bitten you?

der Mouse mouse at mcgill-vision.UUCP
Tue Aug 13 08:14:00 AEST 1985


>	scanf("%D %F", long, double);
>	scanf("%ld %lf", long, double);
[should be &long, &double in both cases]
> vs.
>	printf("%ld %f", long, double);

> Why no %F or %D on printf?

Good question.  Belongs there.  So don't use it in scanf and there's no
problem.

> And why %lf vs %f? fun!

Disclaimer first:  What I say here is based on my hacking on a VAX.
Lots of my comments may well be invalid elsewhere.

The C compiler produces exactly the same code for
	printf(format,long,double)
as
	printf(format,long,float)

Remember in K&R how all floats are converted to doubles all the time?  This
also happens in function calls.  Printf may support %lf; I haven't checked.
But it would necessarily be treated exactly the same as %f because of this
extension.  Scanf does not have the same problem (feature?) because you pass
a pointer, you don't pass the value directly.

By the way (this is very VAX-dependent), you can scanf into a double and tell
scanf it's a float (use %f rather than %lf).  This works because the first 4
bytes of a double form a valid float.  The extra precision will be unchanged,
but for user input, the data generally isn't that precise anyway.
-- 
					der Mouse
				System hacker and general troublemaker
				CVaRL, McGill University

Hacker: One responsible for destroying /
Wizard: One responsible for recovering it afterward



More information about the Comp.lang.c mailing list