Don't use Scanf()

Doug Gwyn gwyn at brl-smoke.ARPA
Sat Mar 19 12:05:49 AEST 1988


In article <1140 at csun.UUCP> sef at csun.UUCP (Sean Fagan) writes:
>For the most part, I don't include <stdio> in my programs at
>all; printf doesn't need it, and I can get by with write very easily.

printf() most certainly does need <stdio.h> included to correctly
declare its type, in general; if you haven't gotten into trouble
by not having a variadic declaration in scope, it's only because
you've been using implementations on relatively simple architectures.

write(1,...) is not at all standard, so if you're trying to write
portable applications you should stick with the standard library.

You should also realize that in most cases it is more important for
the source code to be maintainable that to squeeze the last bit of
performance out of the hardware.  This lesson was learned by most
professionals in the late 1960s and early 1970s.

Finally, your intuitions about what is "efficient" are wrong.  I
ran a test that simply printed "Hello, world!\n" 100,000 times on
the standard output, on a Gould PN9080 using my SVR2 emulation,
which has essentially the UNIX System V stdio implementation.  I
tried it three ways, using printf, fputs to stdout, and write to
file descriptor 1 (in all cases using /dev/null as the output file).
Here are the results:

	printf	 5.5 seconds (user + system)
	fputs	 4.0 seconds (user + system)
	write	14.2 seconds (user + system)

You do gain a little by using fputs, although in a normal application
(rather than an output test program) the difference would be not be
significant.  Note that write(1,...) is a major lossage since you lose
buffering.



More information about the Comp.lang.c mailing list