473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 2817
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)cyberspace.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_SUCCESS);
}

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 "implementation 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******@spamcop.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 "implementation 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)cyberspace.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
Kenneth Brody a écrit :
Or did you mean "s/same/different/"?


No. Just a bad wording. I have given a detailed explanation in my reply
to Randy.

--
C is a sharp tool
Nov 15 '05 #11
Kenneth Brody a écrit :
Or did you mean "s/same/different/"?


No. Just a bad wording. I have given a detailed explanation in my reply
to Jirka.

--
C is a sharp tool
Nov 15 '05 #12
On Thu, 20 Oct 2005 19:06:02 +0200, Emmanuel Delahaye
<em**********@noos.fr> wrote in comp.lang.c:
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.


[snip]

No, they are not, even leaving aside the lack of a newline in the
first example. Here are two programs that are guaranteed to have the
same behavior:

#include <stdio.h>
int main(void)
{
printf("%d\n", 10);
return 0;
}

....and:

#include <stdio.h>
int main(void)
{
printf("%d\n", 10);
fflush(stdout);
return 0;
}

The call to fflush(stdout), or alternatively fflush(NULL), just before
main() returns is absolute redundant and accomplishes nothing.

Part of 7.19.3 p5:

"If the main function returns to its original caller, or if the exit
function is called, all open files are closed (hence all output
streams are flushed) before program termination."

And neither one of the above programs guarantees that the output will
appear, on a device or in a file.

In reality, there are platform/terminal driver combinations that do
not issue a newline before a prompt, so there are some *NIX
environments where you could see something like this:

10$prompt

....but it might not appear at all.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #13
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);
The programmers you are listening to are wrong with one exception.
There is no reason not to use printf() to write partial lines. You can
use this to build up lines without complicated logic.
Consider a simple case, in which we print several lines of ints:

#include <stdio.h>
void example(void)
{
int i;
for (i = 0; i < 128; i++)
printf("%3d%c", i, (7 == i%8) ? '\n' : ' ');
}

This code has a trailing '\n' for only 1/8 of the lines. It can, of
course, have been written

#include <stdio.h>
void example(void)
{
int i;
for (i = 0; i < 128; i++) {
printf("%3d", i;
if (7 == i%8) putchar('\n');
else putchar(' ');
}
}

Now for the exception. It is implementation-defined what happens if you
do not terminate the last line of output with an end-of-line character.
It is not safe to leave out this last '\n' unless you are writing for a
particular environment and C implementation, you *know* what happens in
that environment and with that implementation, you *know* that neither
that environment nor implementation will ever change (via updates, etc.)
in its interpretation of leaving off that final '\n', and you *know*
that you will not need your program to ever run in any other configuration.
It's application specific, isn't it?


No.
Nov 15 '05 #14
Emmanuel Delahaye:
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.
If the printf above is intended to be the last output of a program,
the fflush(stdout) gains you nothing, since it is performed anyway
at [with/by?] program termination. If the "10" appears on the output
or not is implementation-defined, as you mentioned yourself. Therefore,
the behaviour is not reproducable.
You are welcome to rephrase that in plain and simple English.


If I ever find out what exactly you meant, I'll try. ;-)

Jirka
Nov 15 '05 #15

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

Similar topics

11
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 <=...
8
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
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 %%...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
29
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...
4
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
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...
19
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,...
34
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.