473,836 Members | 2,292 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 35574
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?
Aug 25 '06 #21
jmcgill wrote:
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?
Doing so at the user level is expressly forbidden by the standard.

--
Chuck F (cb********@yah oo.com) (cb********@mai neline.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.netUSE maineline address!
Aug 25 '06 #22
CBFalconer wrote:
jmcgill wrote:
>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?

Doing so at the user level is expressly forbidden by the standard.

Fair enough. But still would not appreciate the idea of the compiler
snooping into my function arguments and deciding for me if they are needed.

Does GCC do this?
Aug 25 '06 #23
jmcgill <jm*****@email. arizona.eduwrit es:
CBFalconer wrote:
>jmcgill wrote:
>>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?

Doing so at the user level is expressly forbidden by the standard.

Fair enough. But still would not appreciate the idea of the compiler
snooping into my function arguments and deciding for me if they are needed.

Does GCC do this?
1) Yes it does, but that's off-topic.
2) Your compiler needs to see your paramaters in order to compile. Unless
you're hiding drugs in them, there's no reason for you not to want the
compiler searching them.

--
Andrew Poelstra <http://www.wpsoftware. net/projects>
To reach me by email, use `apoelstra' at the above domain.
"Do BOTH ends of the cable need to be plugged in?" -Anon.
Aug 25 '06 #24
Andrew Poelstra wrote:
>Fair enough. But still would not appreciate the idea of the compiler
snooping into my function arguments and deciding for me if they are needed.

Does GCC do this?

1) Yes it does, but that's off-topic.
I figured.
2) Your compiler needs to see your paramaters in order to compile. Unless
you're hiding drugs in them, there's no reason for you not to want the
compiler searching them.
Still bothers me. I don't mind the preprocessor seeing this, and maybe
having it feed the compiler what it wants. But what we are talking
about here is the compiler itself evaluating string literals and making
arbitrary changes to them. It bugs me in a philosophical way, even
though I don't care in any practical sense.
Aug 25 '06 #25


jmcgill wrote On 08/25/06 11:12,:
[... about substitution of Standard functions based on arguments ...]

Fair enough. But still would not appreciate the idea of the compiler
snooping into my function arguments and deciding for me if they are needed.
If I write pow(x,5) it does not bother me in the least if
the compiler generates a call to _intpow(x,5), a hypothetical
optimized-for-integer-powers version.

If I write strlen("Hello, world!") I am delighted if the
compiler just generates (size_t)13.

If I write memcpy(p, q, sizeof *p) I applaud the compiler
that generates a block-move instruction instead of a function
call. For div(num, den) I would be disappointed if the compiler
did *not* generate an in-line division. I *want* sqrt() to
turn into an FSQRT instruction when it can.

Why should printf() be any different?

--
Er*********@sun .com

Aug 25 '06 #26
CBFalconer wrote:
>
Debaser wrote:

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");
... snip ...

p.s. The book is by Herb Schlidt (or something similar) if
that's any indication.

It is. BullSchildt is normally error infested. In this particular
case he is right. This is analagous to undefined behaviour -
sometimes you get the expected result.
And newbie folks need to understand: 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...

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Aug 25 '06 #27
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?
Aug 25 '06 #28


jmcgill wrote On 08/25/06 13:12,:
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?
The former.

I'm having some fun trying to imagine the documentation
if the latter were true. "If the program dereferences a
NULL pointer the system *may* nudge nudge raise a signal
that *might* wink wink be called SIGSEGV, but -- say no
more, say no more, you *didn't* hear that from *me*, eh?"

--
Er*********@sun .com

Aug 25 '06 #29
Charles Richmond wrote:

<snip>
And newbie folks need to understand: 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...
Don't count on not getting the result you expect either. I.e. don't
assume that just because your program gave the correct result it is
necessarily correct. It could still contain undefined behaviour and only
fail when it is most important to you that it works, for example when
you are doing a demonstration to win that multi-million pound bid.
--
Flash Gordon.
Aug 25 '06 #30

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

Similar topics

24
5888
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
17481
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
4
1850
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
15503
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
5613
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
2864
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
2682
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
18058
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
6866
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
9810
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
9656
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
10821
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
10527
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10573
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9358
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5812
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4443
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
2
4001
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.