473,854 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

puts() vs printf()

I've recently read in one of my old C books that puts() is a better
function call with regard to performance than printf() in the
following situation:

puts("Some random text");

vs.

printf("Some random text\n");

[Read: no formatting.]
Would anyone please confirm or deny this? It makes sense since
printf() uses vargs, but I'd like some confirmation. The author uses
void main() so I'm not sure what to think. Given today's hardware and
compiler optimizations I have no idea if it even matters. I'm not very
good at modern-day assembly, so I can't compare it that way.
I've generally steered away from the puts-gets I/O functions in the
past. I don't know why this is the case...maybe it has to do with
consistency as I don't always want the newline automatically output.

What's the word?

Thanks.

p.s. The book is by Herb Schlidt (or something similar) if that's any
indication.
Aug 24 '06
36 35589
jmcgill wrote:
Charles Richmond wrote:
>Undefined Behavior means
that you can get *any* result, including sometimes the result
that you expected. Just don't count on getting the result that
you expected...

Would you say that undefined behavior also entitles an implementor
to define the behavior as appropriate for a given implementation,
or would you say that undefined behavior should remain *expressly*
undefined, and should *not* be defined by any given implementation?
It's undefined from the point of view of the C standard. Just
because the implementor arranges for a particular response doesn't
mean that the same will occur on another system.

--
Some informative links:
news:news.annou nce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
Aug 25 '06 #31
"jmcgill" <jm*****@email. arizona.eduwrot e in message
news:DQvHg.8118 $cw.7613@fed1re ad03...
Stephen Sprunk wrote:
>A modern compiler will notice when a single argument to printf()
contains no format specifiers

I think that would bother me because I consider the compiler knowing
this much about a parameter to a library function to be pathological
binding. I realize it's a very standard library function with
well-defined parameters, but still, this feels like a violation of
some
boundary that shouldn't be crossed. What a nasty surprise if someone
implements his own standard IO library and discovers the compiler has
different ideas about format strings?
The compiler is allowed to "violate that boundary" because the standard
says so. Presumably there's some switch to turn off that optimization
if you don't like it, but why wouldn't you? It only happens in cases
it's proven to be safe, and it follows the as-if rule. There's lots of
scarier/cleverer things that compilers do these days; this is a minor
nit.

If the above offends you, so should GCC complaining if the arguments in
a call to printf() don't match the format string. How is pointing out
bugs a bad thing?

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

Aug 25 '06 #32
"CBFalconer " <cb********@yah oo.comwrote in message
news:44******** *******@yahoo.c om...
Stephen Sprunk wrote:
>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).

It can't. The output of the two statements is different.
The compiler should be able to convert this:

char *foo = "Hello world!\n";
printf(foo);

to this:

char *foo = "Hello world!";
puts(foo);

under the as-if rule, provided there's no other use of foo's contents.
However, proving the latter point may not be worth the effort, so only
things of the form printf("Hello world!\n"); or printf("%s\n",f oo);
would be converted.

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

Aug 25 '06 #33
Stephen Sprunk wrote:
The compiler is allowed to "violate that boundary" because the standard
says so.
I buy it fine. I hadn't fully considered that we're only talking about
standard library calls in the first place.
Aug 26 '06 #34
CBFalconer wrote:
It's undefined from the point of view of the C standard. Just
because the implementor arranges for a particular response doesn't
mean that the same will occur on another system.

My question is more along the lines of this, and I don't think I was
clear enough:

Does an undefined behavior, that is explicitly undefined in the spec (as
opposed to the infinite things being undefined by ommission), mean that
the spec is attempting to require that behavior to be undefined, or is
it open enough that an implementation that defines some specific
behavior for that case is not nonconforming as a result?

Maybe that's still not clear enough.

Aug 26 '06 #35

jmcgill wrote:
Stephen Sprunk wrote:
The compiler is allowed to "violate that boundary" because the standard
says so.

I buy it fine. I hadn't fully considered that we're only talking about
standard library calls in the first place.

The copmiler is also free to do so for non-library functions, within
the bounds set by the as-if rule. So if the defintion of mystrlen() is
adequetely visible to the compiler (and assuming the obvious
implementation of that function), the compiler is free to reduce
"a=mystrlen("so mestring")" to "a=9".

In fact, this sort of optimization is quite common with inlined
functions.

Aug 27 '06 #36
<ro***********@ yahoo.comwrote in message
news:11******** **************@ 75g2000cwc.goog legroups.com...
>
jmcgill wrote:
>Stephen Sprunk wrote:
The compiler is allowed to "violate that boundary" because the
standard
says so.

I buy it fine. I hadn't fully considered that we're only talking
about
standard library calls in the first place.

The copmiler is also free to do so for non-library functions, within
the bounds set by the as-if rule. So if the defintion of mystrlen()
is
adequetely visible to the compiler (and assuming the obvious
implementation of that function), the compiler is free to reduce
"a=mystrlen("so mestring")" to "a=9".

In fact, this sort of optimization is quite common with inlined
functions.
With inlined functions, it's fair to _assume_ your compiler will do such
optimizations; if it doesn't, it's time to upgrade. Better compilers
have "inter-procedural optimization" or "whole-program optimization",
which allow them to do the same thing with non-inline functions within
the same translation unit or even across translation units -- provided,
of course, they can prove the "as if" rule applies.

<OT>The GCC folks have recently started playing with IPO within the same
translation unit, but it's still in it infancy and only seems to work on
inline and static functions so far; many commercial compilers have had
IPO for a while. A few have WPO, but typically that means the "compile"
stage emits some intermediate representation and actual compilation and
most optimization is delayed until the "link" stage. The GCC folks
haven't made any noises they're considering that route, but five years
from now I wouldn't be surprised to see it.</OT>

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

Aug 27 '06 #37

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
5893
by: Charles Ulrich | last post by:
Greetings, I hope my greenness isn't showing too bad by asking this, but I ran across this trivial program today that left me flabbergasted: #define MESSAGE "This account is currently not available.\n" int main(int argc, char *argv) {
188
17500
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
4
1851
by: I_have_nothing | last post by:
Hi! Is there any easy way to printf an integer in a way like 1,234,567? I know "%d" can be usd to print it as 1234567. Any type field in format specification can do that? Or any easy way to do that? How about to do that same thing in float type? such as make 987564321.85 into 987,564,321.85 ?
8
15504
by: jchludzinski | last post by:
Is there a format specification for printf that will result in: 1000000 being printed as 1,000,000? Or 1000000.0 as 1,000,000.0? ---John
69
5618
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after assigning them their maximum defined values. However, the output of printf() for the long double 'ld' and the pointer of type void 'v_p', after initialisation don't seem to be right. The compiler used was gcc (mingw) with '-Wall', '-std=c99' and
18
2869
by: sam_cit | last post by:
Hi Everyone, int main() { printf("not included stdio.h"); } Yes, i haven't included stdio.h and my compiler would generate a warning and would assume that it would return a int, my question is how does the linker manage to link the function invocation to the proper
11
2684
by: vectorizor | last post by:
Hi guys, My program deals with with lots of large matrices. To easily debug it, I would like to be able to dump them in files, so that I can easily import then in other software (think matlab, excel) so I can analyse the intermediate results. To make sure the data can be understood by other programs, a space need to be included between each cell, and a return at the end of each row (row-major order). Hence at the moment, the dump code...
6
18064
by: MikeC | last post by:
Folks, Can the width specifier be used in a printf() text string? If I execute... printf("%4s", ".........."); it prints ten dots. If I execute
20
6870
by: Sanchit | last post by:
I want to know how does printf (stdio library function) works? Does this depand on complier (I am using gcc on Linix) Does it uses some buffer in which it stores all what needed to be printed and in end of program it prints that or something else.
0
9903
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7083
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5753
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5946
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.