Don't use Scanf()

Sean Fagan sef at csun.UUCP
Sun Mar 13 12:56:06 AEST 1988


In article <9241 at sunybcs.UUCP> ugfailau at sunybcs.UUCP (Fai Lau) writes:
>In article <1185 at ucsfcca.ucsf.edu> roland at rtsg.lbl.gov (Roland McGrath) writes:
[regarding uses of printf(3)]

>	C'mon!!! Is it really an issue?

Yes, it can be; since printf(3) is interpreted, it *WILL* be slower than
directly coding the correct statements.  I.e., printf("Hello world!\n") is
slower than puts("Hello world!") which is slower than 
write(1,"Hello world!\n",13).  If you're doing a lot of I/O like this, it can
be significantly slower.

>>For example: [printf vs. puts]
>	I can't imagine any reasonably competant C compiler not
>generating the almost same codes for both cases.

What does a compiler have to do with a *subroutine*?!  printf is a
subroutine, just like any other; the compiler knows nothing about it, nor
should it.  (As an aside, I've thought about writing a printf compiler, but
has anybody else already done that?  The generated code would be larger,
but, most likely, quite a bit faster...)

>	<stdio.h> should be included in mostly every program
>just so that when you do need it you have it around. It's not
>gonna give you a bigger or slower program.

No, but it does eat up namespace, typedef space, you're precluded from using
your own putc, getc, etc., etc.

>>And the ultimate stupidity:
>>	printf("\n");
>>Get a brain!!!!!  You're using the function that can do complex
>>data formatted output to write one bleeping character!!!!!
>>Try
>>	putchar('\n');
>>If you include <stdio.h>, this will probably end up being a macro
>>that will write it out very fast.
[sprintf(buf, "constant string"); instead of strcpy]
>	Let's face it. The best way to give efficiency to a program
>is to write your own macro to handle file I/O and strings. Macros like
>strcpy are too general if you want to be picky. 
[write own routine using putc for fast I/O]

No, if one wants to do that, one doesn't use <stdio> at all; one uses
write(2) (assuming you're on Unix; however, most PC C compilers support
write anyway).  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.

>Fai Lau
>SUNY at Buffalo (The Arctic Wonderland)
>UU: ..{rutgers,ames}!sunybcs!ugfailau
>BI: ugfailau at sunybcs INT: ugfailau at joey.cs.buffalo.EDU


-- 

Sean Fagan                   uucp:   {ihnp4,hplabs,psivax}!csun!sef
CSUN Computer Center         BITNET: 1GTLSEF at CALSTATE
Northridge, CA 91330         (818) 885-2790



More information about the Comp.lang.c mailing list