Re: printf()



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
.



Relevant Pages

  • Small Correction to K&R Exercise 1-22 Solution on CLC-Wiki
    ... lines after the last non-blank character that occurs before the n-th ... int t,len; ... This space will be replaced by a newline, ...
    (comp.lang.c)
  • Re: SHOW DEVICE/FILES with FORTRAN?
    ... >> I don't think that locks are involved. ... to to find a list of open files. ... typedef struct {unsigned int len; void *address;} desc; ... printf ("The search_devnam argument accepts the standard wildcard ...
    (comp.os.vms)
  • Re: ignore a line
    ... int main ... Without a terminating newline in the printf string, ... You would set a flag before beginning the copy, ... clear the flag and skip writing that line to the destination file. ...
    (comp.lang.c)
  • Re: ignore a line
    ... int main ... Without a terminating newline in the printf string, ... You would set a flag before beginning the copy, ... clear the flag and skip writing that line to the destination file. ...
    (alt.comp.lang.learn.c-cpp)
  • modified "histogram" exercise from K&R2
    ... second word by writing equal number of stars, *, at the output. ... "newline in output". ... each extra space, tab and newline appears in the output ... int main ...
    (comp.lang.c)