printf()

  • Thread starter Turner, GS \(Geoff\)
  • Start date
T

Turner, GS \(Geoff\)

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
 
P

paulius-maruska

Turner said:
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.
 
C

Christopher Benson-Manica

Turner said:
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.
 
K

Kenneth Brody

paulius-maruska said:
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:[email protected]>
 
C

Christopher Benson-Manica

Kenneth Brody said:
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)
 
E

Emmanuel Delahaye

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.
 
J

Jirka Klaue

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
 
R

Randy Howard

Emmanuel Delahaye wrote
(in article said:
Turner, GS (Geoff) a écrit :

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.
 
K

Kenneth Brody

Emmanuel said:
Turner, GS (Geoff) a écrit :

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:[email protected]>
 
M

Martin Ambuhl

Turner said:
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.
 
E

Emmanuel Delahaye

Jirka Klaue a écrit :
Emmanuel Delahaye:



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
 
J

Jack Klein

Turner, GS (Geoff) a écrit :

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.
 
J

Jirka Klaue

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,924
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top