Re: puts() vs printf()



"Schraalhans Keukenmeester" <firstname_dot_lastname@xxxxxxxxxxxxxxx>
wrote in message news:44edeb63$0$4517$e4fe514c@xxxxxxxxxxxxxxxxx
Stephen Sprunk wrote:
A modern compiler will notice when a single argument to printf()
contains no format specifiers and replace the call with puts() --
assuming it can strip the final '\n' at compile time.

I personally prefer using printf() for all output because then I
don't
have to deal with forgetting the implicit '\n' that puts() adds.

Slightly wandering OT: Tried my example (see my other response in this
thread) with gcc optimization enabled and ONLY when the string is a
literal or a #defined constant it looks as if execution times are
identical, which may be (and probably is) indicative of printf() being
replaced by the faster puts(). Declaring the string (even as const)
*char does not convince gcc to use puts() in stead of printf(), at
least
not here.

Would you say that gcc is a modern or old compiler based on this?
Sh.

My experience plus Racaille's report in another post lead me to say it's
modern, i.e. doing something reasonably clever as I described. It's
curious that it doesn't convert a printf(char*) to puts(char*) but does
with literals, but there may be some reason for that or it might be a
bug (at least in your version).

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking



--
Posted via a free Usenet account from http://www.teranews.com

.



Relevant Pages

  • Re: [9fans] new contest
    ... nm shows that printf gets changed for puts, ... main(int argc, char *argv) ... quoting some anonymous gcc developer ...
    (comp.os.plan9)
  • OT - Re: Convert output from MD5 to printable ascii
    ... to avoid linking in all of printf() unnecessarily. ... I presume you would first confirm that the use of puts() doesn't ... object code size. ...
    (comp.lang.c)
  • Re: Convert output from MD5 to printable ascii
    ... I presume you would first confirm that the use of puts() doesn't ... reason is that printf is a gargantuan link-in in comparison to puts. ... I just tried this with gcc 4.0.1 using gcc on a Hello, ... and that output file went up by 4284 bytes. ...
    (comp.lang.c)
  • Re: Hardcoded insert string for the event messages
    ... This isn't what you'll see, but FWIW, here's what gcc does with it: ... Note that gcc has replaced the call to printf with a call to puts. ...
    (comp.lang.asm.x86)
  • Re: Convert output from MD5 to printable ascii
    ... I presume you would first confirm that the use of puts() doesn't ... I don't know what quality of implementations you are used to. ... All the compilers I use basically do link these seperately. ... reason is that printf is a gargantuan link-in in comparison to puts. ...
    (comp.lang.c)