why use puts instead of printf? 
Author Message
 why use puts instead of printf?

There's a program that I inherited that simply outputs a text file
with certain values. I'm curious as to the programmers motives for
using puts instead of printf below. Anyone care to speculate?

   puts( "data_product=MODEL OUTPUT::" );
   puts( "granule_size=5000000::" );
   printf( "begin_date=%s::\n", BegDate );
   printf( "end_date=%s::\n",   EndDate );
   puts( "geog_flag=g::" );
   puts( "lat1=-89.5::" );
   puts( "lon1=-179.5::" );
   puts( "lat2=89.5::" );
   puts( "lon2=-179.5::" );
   puts( "lat3=89.50::" );
   puts( "lon3=179.50::" );
   puts( "lat4=-89.50::" );
   puts( "lon4=179.50::" );
   puts( "day_night_flag=-::" );
   puts( "granule_version=01::" );
   puts( "miss_val_flt=-999.9::" );
   puts( "miss_val_int=0::" );
   if (argc == 4)  puts( satlineout );
   printf( "producer_granule_id=%s::\n", ProducerGranuleId );
   puts("time_comment = for begin and end time HH:00:00, HH denotes time of ");
   puts("instantaneous observation for prognostic variables, 3 hour average ");
   puts("ending at time HH for surface diagnostic variables, and a 6 hour   ");
   puts("average centered at time HH for upper air diagnostic variables     ");

--

 (301) 286-2396                          fax: (301) 286-1754

"Tut! Tut!" cried Sherlock Holmes. "You must act, man, or you are
lost. Nothing but energy can save you. This is no time for dispair."



Mon, 24 Mar 1997 22:59:57 GMT  
 why use puts instead of printf?
fputs can copy its input directly to the buffer, no need for
interpretation.

No need to escape embedded '%'.

No need for an explicit newline at the end of the string (not
necessarily an advantage).

--
----------------------------------------+----------------------------------

FREE THE FORTUNE 500!!  N O   IA, of the|             Cupertino, California
NSA, FBI, aNeitzche iB I L L SGuniverse!| (xxx)xxx-xxxx               95013



Tue, 25 Mar 1997 06:40:46 GMT  
 why use puts instead of printf?

Quote:

>There's a program that I inherited that simply outputs a text file
>with certain values. I'm curious as to the programmers motives for
>using puts instead of printf below. Anyone care to speculate?

>   puts( "geog_flag=g::" );   [vs. printf( "geog_flag=g::\n");]
> [etc.]

puts is a lot faster because it doesn't have to scan for "%" symbols
while output, it only has to scan for '\0'.  What kind of real effect
does that have?  Well, I tested it on an AXP system, timing a whole
bunch of puts calls and a whole bunch of printf calls.  (Actually, I
used fputs and fprintf to a file so that screen display times wouldn't
swamp the difference.)  

With each, I used the string "this is a test line\n".  Since the
difference is based on per-character processing, there should be a
greater difference with longer strings.

fputs: 6.9 usec per call
fprintf: ~13 usec per call

fputs was relatively predictable in the amount of time, fprintf varied
more.  I don't know why that was.

What about in this case where one is printing to the screen?  Terminal
scrolling will slow both of them down, but there's still a difference in
the amount of CPU used.  While the time the person will take to read the
information on the screen far outweighs the difference in processing
time, there are still two good reasons for being careful like this:
getting in a good habit of not using 'wasteful' functions; and there are
some people that have to pay for their CPU cycles.

-michael j zehr



Tue, 25 Mar 1997 04:04:32 GMT  
 why use puts instead of printf?

|There's a program that I inherited that simply outputs a text file
|with certain values. I'm curious as to the programmers motives for
|using puts instead of printf below. Anyone care to speculate?
|
|   puts( "data_product=MODEL OUTPUT::" );
|   puts( "granule_size=5000000::" );

[ rest of example deleted ]

Allow me to give an analogy, just imagine I'd like to know
the tangent of a (floating point) number. There are a couple
of alternatives:

- use the tan() function, which can give me just what I want;
- use sin()/cos(), which can do the job too;
- come up with some home brew thingy that hopefully does the job;
- type in a huge table with all sorts of function values;
- etc. etc.

Here the solution is straightforward: alternative #1 is the
solution here; and the same reasoning applies to your example:
if you want to output a string, terminated with a newline on
stdout, puts() is your friend ...

On the other hand, if you want to fiddle diddle with cleverly
constructed macros, that chang all your printf() calls to something
debugging like, this simple puts() function has to be dealt with
separately.

But, if all you want is a string printed on stdout, terminated
by a line feed and a carriage return, why not simply use puts()?
Nothing wrong with that ...

kind regards,




Tue, 25 Mar 1997 00:27:34 GMT  
 why use puts instead of printf?

Quote:

> There's a program that I inherited that simply outputs a text file
> with certain values. I'm curious as to the programmers motives for
> using puts instead of printf below. Anyone care to speculate?

>    puts( "data_product=MODEL OUTPUT::" );
>    puts( "granule_size=5000000::" );
>    printf( "begin_date=%s::\n", BegDate );
>    printf( "end_date=%s::\n",   EndDate );

Yes, sure. The function printf() has more overhead than puts() does.
printf() has to check every character in the format string for '%' while
puts() can just blast its argument into the buffer, or hand to write()
in case of unbuffered I/O. Oh, and fputc() a '\n' after it. If an
application does little more than printing this can make a difference.
If your application actually *does* something, however, one will tend
to notice less and less of a difference. It all depends.

--
                                       ^^

University of Twente                 =x=  \        tel. +31 53 893747
Tele-Informatics & Open Systems        |   \       tfx. +31 53 333815
P.O. Box 217   7500 AE Enschede       /|__  \
The Netherlands                      (____)_/

I have yet to see any problem, however complicated, which, when looked
at in the right way, did not become still more complicated.
                -- Poul Anderson



Tue, 25 Mar 1997 02:51:15 GMT  
 why use puts instead of printf?

Quote:
>No need for an explicit newline at the end of the string (not
>necessarily an advantage).

When you don't need the newline, you can always use fputs.

This is the BUGS section in the f/puts man page on one of my systems:

BUGS
     puts appends a newline, fputs does not, all in the name of backward
     compatibility.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Wed, 26 Mar 1997 03:58:06 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Why you should use puts(3) when you don't need printf(3)

2. Putting Propertysheet onto a CFormview instead of Dialog!!

3. How to put bitmaps on dialog box as hotspots instead of bitmap-buttons

4. printf() and puts()

5. printf, puts, gets etc don't work when stepping into

6. I/O with puts/printf

7. what is the difference between puts and printf ?

8. printf() vs. puts()

9. VC7: cstdio doesn't put printf in std

10. Why Use `Int` Instead

11. Why print out -1 instead of 3?

12. why some use CFormView or CDaoRecordView instead?

 

 
Powered by phpBB® Forum Software