473,811 Members | 3,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printf()

Out of curiosity, why do programmers
insist that the following piece of code
printf("%d", 10);
is wrong and should be
printf("%d\n", 10);

It's application specific, isn't it?

GST

Nov 15 '05 #1
14 2876
Turner, GS (Geoff) wrote:
Out of curiosity, why do programmers
insist that the following piece of code
printf("%d", 10);
is wrong and should be
printf("%d\n", 10);

It's application specific, isn't it?

GST


It isn't wrong - it compiles, it links, it runs.
But in real situations, i think there are a lot more cases when
programmers simply forget to put \n, so if you have it in loop (that
should print, say, 20 integers) - output will be realy ugly and you
won't be able to find *what is what* in the output.

Nov 15 '05 #2
"Turner, GS \(Geoff\)" <G.********@rl. ac.uk> wrote:
Out of curiosity, why do programmers
insist that the following piece of code
printf("%d", 10);
is wrong and should be
printf("%d\n", 10); It's application specific, isn't it?


Of course. There is, however, at least one situation where it may
very well be Wrong.

#include <stdio.h>

int main( void )
{
printf("%d", 10);
return 0;
}

It is implementation-defined whether the last line of output requires
a newline.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #3
paulius-maruska wrote:

Turner, GS (Geoff) wrote:
Out of curiosity, why do programmers
insist that the following piece of code
printf("%d", 10);
is wrong and should be
printf("%d\n", 10);

It's application specific, isn't it?

GST


It isn't wrong - it compiles, it links, it runs.
But in real situations, i think there are a lot more cases when
programmers simply forget to put \n, so if you have it in loop (that
should print, say, 20 integers) - output will be realy ugly and you
won't be able to find *what is what* in the output.


That, and sometimes people give a small sample program to demonstrate
a problem/issue/question, and include a single printf() without the
newline. For example, suppose the above line were the complete body
of main():

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
print("%d",10);
exit(EXIT_SUCCE SS);
}

According to the standard, if the output of the program does not end
in a newline, the output is not guaranteed to appear. (I don't recall
if it's "undefined" or "implementa tion defined".)

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Nov 15 '05 #4
Kenneth Brody <ke******@spamc op.net> wrote:
According to the standard, if the output of the program does not end
in a newline, the output is not guaranteed to appear. (I don't recall
if it's "undefined" or "implementa tion defined".)


C99 7.19.2:

2 A text stream is an ordered sequence of characters composed
into lines, each line consisting of zero or more characters
plus a terminating new-line character. Whether the last line
requires a terminating new-line character is
implementation-defined.
^^^^^^^^^^^^^^^ ^^^^^^^

(Quoted by Ben Pfaff at http://snipurl.com/iqxx)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #5
Turner, GS (Geoff) a écrit :
Out of curiosity, why do programmers
insist that the following piece of code
printf("%d", 10);
is wrong and should be
printf("%d\n", 10);
It's not wrong, it's just that the effect is not portable.

printf("%d", 10);
fflush (stdout);

and

printf("%d\n", 10);

are guaranteed to have the same behaviour whatever the implementation.

It's application specific, isn't it?


Application is irrelevent on c.l.c.

--
C is a sharp tool
Nov 15 '05 #6
Emmanuel Delahaye:
printf("%d", 10);
fflush (stdout);

and

printf("%d\n", 10);

are guaranteed to have the same behaviour whatever the implementation.


Did you forget to add: ... except that no new-line is printed by the
first snippet?

Jirka
Nov 15 '05 #7
Emmanuel Delahaye wrote
(in article <43************ **********@news .free.fr>):
Turner, GS (Geoff) a écrit :
Out of curiosity, why do programmers
insist that the following piece of code
printf("%d", 10);
is wrong and should be
printf("%d\n", 10);


It's not wrong, it's just that the effect is not portable.

printf("%d", 10);
fflush (stdout);

and

printf("%d\n", 10);

are guaranteed to have the same behaviour whatever the implementation.


Not the same, but very similar. That newline makes a
difference.
--
Randy Howard (2reply remove FOOBAR)

Nov 15 '05 #8
Emmanuel Delahaye wrote:

Turner, GS (Geoff) a écrit :
Out of curiosity, why do programmers
insist that the following piece of code
printf("%d", 10);
is wrong and should be
printf("%d\n", 10);


It's not wrong, it's just that the effect is not portable.

printf("%d", 10);
fflush (stdout);

and

printf("%d\n", 10);

are guaranteed to have the same behaviour whatever the implementation.

[...]

Then I guess I need to get my money back on all these platforms where the
second one outputs a newline that isn't there in the first one.

Not to mention the fact that the missing newline on the first means that
(as I understand it) it is possible that no output appears on some
platforms.

Or did you mean "s/same/different/"?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Nov 15 '05 #9
Jirka Klaue a écrit :
Emmanuel Delahaye:
printf("%d", 10);
fflush (stdout);

and

printf("%d\n", 10);

are guaranteed to have the same behaviour whatever the implementation.

Did you forget to add: ... except that no new-line is printed by the
first snippet?

Jirka


No, But when I have reread my post, I have discovered that my intention
was badly worded. I meant that:

On one hand

printf("%d", 10);
fflush (stdout);

and on the other hand

printf("%d\n", 10);

are individually guaranteed to have a well defined and reproductable
behaviour. I don't mean that both have the same behaviour. You are
welcome to rephrase that in plain and simple English.

Thanks
--
C is a sharp tool
Nov 15 '05 #10

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

Similar topics

11
5959
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <= 4 */ };
8
2724
by: aditya | last post by:
hi, Can anybody please tell me that how the following printf(...) statement works- main(){ int d=9; printf("%d",printf("%d")); return 0;
7
96340
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me for posting without verfying things with any standard compiler, i don't have the means for now.
188
17471
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
29
17632
by: whatluo | last post by:
Hi, c.l.cs I noticed that someone like add (void) before the printf call, like: (void) printf("Timeout\n"); while the others does't. So can someone tell me whether there any gains in adding (void) to printf. Thanks, Whatluo.
4
2013
by: pai | last post by:
Hi , Can any one tell me how this statement of printf is behaving . how the last digit is printed int a=2,b=4,c=7; printf("%d",printf("%d %d:",a,b)); //answer to this was 2 4:3
11
3929
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try to do some example to explain, However, after some trying, I confused when I try to do some integer constant division, I know I should do float division something like 3.0/6.0, but I'm still confused where the -0.124709 comes for the following...
19
9927
by: RedDevilDan | last post by:
I am working on a Memory Footprint Reduction project. I came across an idea to disable all printf statements so that less memory is required. In addition, when there is no single printf statement, the printf library will not be linked, so it further reduces the executable size. Is there an easy way to disable printf in a large project? In my project, we have thousands of C & C++ files. They don't have a common included common header. ...
34
16015
by: Old Wolf | last post by:
Is there any possible situation for printf where %hd causes a different result to %d, and the corresponding argument was of type 'short int' ?
1
8526
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
0
10647
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
10386
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...
0
9204
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...
1
7669
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
6889
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
5554
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
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.