Re: printf()
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Thu, 20 Oct 2005 22:03:08 -0500
On Thu, 20 Oct 2005 19:06:02 +0200, Emmanuel Delahaye
<emdelYOURBRA@xxxxxxx> 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/~ajo/docs/FAQ-acllc.html
.
- References:
- printf()
- From: Turner, GS \(Geoff\)
- Re: printf()
- From: Emmanuel Delahaye
- printf()
- Prev by Date: Re: how to determine a string to be a number?
- Next by Date: Re: how to use pure c to write OO programs
- Previous by thread: Re: printf()
- Next by thread: Re: printf()
- Index(es):
Relevant Pages
|