Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > printf() in a loop without \n won't display anything till the end

Reply
Thread Tools

printf() in a loop without \n won't display anything till the end

 
 
Michel Rouzic
Guest
Posts: n/a
 
      12-08-2005
With the following code :

int main()
{
int i, j, n;

n=12;
for (i=0; i<n; i++)
{
printf("Iteration %i out of %i\n", i+1, n);
for (j=0; j<100000000; j++) {}
}
return 0;
}

With \n at the end of the line to be printed, everything will printf
when it should
With \r intead of \n, or just no \n or \r, nothing will get displayed
before the loop ends (it will print it all at once, or if there's a \r,
only the last remaining line)

Does anyone know the origin of this somewhat weird behavior, and does
anyone know how to fix it so I can display the progress of my
iterations on only one line (by using \r at the end) ?

 
Reply With Quote
 
 
 
 
Tatu Portin
Guest
Posts: n/a
 
      12-08-2005
Michel Rouzic wrote:

> With the following code :
>
> int main()
> {
> int i, j, n;
>
> n=12;
> for (i=0; i<n; i++)
> {
> printf("Iteration %i out of %i\n", i+1, n);
> for (j=0; j<100000000; j++) {}
> }
> return 0;
> }
>
> With \n at the end of the line to be printed, everything will printf
> when it should
> With \r intead of \n, or just no \n or \r, nothing will get
> displayed before the loop ends (it will print it all at once, or if
> there's a \r, only the last remaining line)
>
> Does anyone know the origin of this somewhat weird behavior, and
> does anyone know how to fix it so I can display the progress of my
> iterations on only one line (by using \r at the end) ?



Use fflush(stdout) if you want everything printed at certain point.


--
C faq: http://www.eskimo.com/~scs/C-faq/top.html
Reference: http://www.acm.uiuc.edu/webmonkeys/book/c_guide/
Coding standards: http://www.psgd.org/paul/docs/cstyle/cstyle.htm
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      12-08-2005
Michel Rouzic said:

> With \r intead of \n, or just no \n or \r, nothing will get displayed
> before the loop ends (it will print it all at once, or if there's a \r,
> only the last remaining line)


printf("Iteration %i out of %i\r", i+1, n);
fflush(stdout);

There are some gotchas, but I don't suppose they're likely to worry you at
this stage.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Anand
Guest
Posts: n/a
 
      12-08-2005
Michel Rouzic wrote:
> With the following code :
>
> int main()
> {
> int i, j, n;
>
> n=12;
> for (i=0; i<n; i++)
> {
> printf("Iteration %i out of %i\n", i+1, n);
> for (j=0; j<100000000; j++) {}
> }
> return 0;
> }
>
> With \n at the end of the line to be printed, everything will printf
> when it should
> With \r intead of \n, or just no \n or \r, nothing will get displayed
> before the loop ends (it will print it all at once, or if there's a \r,
> only the last remaining line)
>
> Does anyone know the origin of this somewhat weird behavior, and does
> anyone know how to fix it so I can display the progress of my
> iterations on only one line (by using \r at the end) ?
>

Both are covered in the FAQ.

--
(Welcome) http://www.ungerhu.com/jxh/clc.welcome.txt
(clc FAQ) http://www.eskimo.com/~scs/C-faq/top.html
 
Reply With Quote
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      12-08-2005

Michel Rouzic wrote:
> With the following code :
>
> int main()
> {
> int i, j, n;
>
> n=12;
> for (i=0; i<n; i++)
> {
> printf("Iteration %i out of %i\n", i+1, n);
> for (j=0; j<100000000; j++) {}
> }
> return 0;
> }
>
> With \n at the end of the line to be printed, everything will printf
> when it should
> With \r intead of \n, or just no \n or \r, nothing will get displayed
> before the loop ends (it will print it all at once, or if there's a \r,
> only the last remaining line)
>
> Does anyone know the origin of this somewhat weird behavior, and does
> anyone know how to fix it so I can display the progress of my
> iterations on only one line (by using \r at the end) ?


See page 270, N869
"When a stream is unbuffered, characters are intended to appear from
the source or at the
destination as soon as possible. Otherwise characters may be
accumulated and
transmitted to or from the host environment as a block. When a stream
is fully buffered,
characters are intended to be transmitted to or from the host
environment as a block when
a buffer is filled. When a stream is line buffered, characters are
intended to be
transmitted to or from the host environment as a block when a new-line
character is
encountered. Furthermore, characters are intended to be transmitted as
a block to the host environment when a buffer is filled, when input is
requested on an unbuffered stream, or when input is requested on a line
buffered stream that requires the transmission of
characters from the host environment. Support for these characteristics
is
implementation-defined, and may be affected via the setbuf and setvbuf
functions."

 
Reply With Quote
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      12-08-2005

Michel Rouzic wrote:
> With the following code :
>
> int main()
> {
> int i, j, n;
>
> n=12;
> for (i=0; i<n; i++)
> {
> printf("Iteration %i out of %i\n", i+1, n);
> for (j=0; j<100000000; j++) {}
> }
> return 0;
> }
>
> With \n at the end of the line to be printed, everything will printf
> when it should
> With \r intead of \n, or just no \n or \r, nothing will get displayed
> before the loop ends (it will print it all at once, or if there's a \r,
> only the last remaining line)
>
> Does anyone know the origin of this somewhat weird behavior, and does
> anyone know how to fix it so I can display the progress of my
> iterations on only one line (by using \r at the end) ?


See page 270, N869
"When a stream is unbuffered, characters are intended to appear from
the source or at the
destination as soon as possible. Otherwise characters may be
accumulated and
transmitted to or from the host environment as a block. When a stream
is fully buffered,
characters are intended to be transmitted to or from the host
environment as a block when
a buffer is filled. When a stream is line buffered, characters are
intended to be
transmitted to or from the host environment as a block when a new-line
character is
encountered. Furthermore, characters are intended to be transmitted as
a block to the host environment when a buffer is filled, when input is
requested on an unbuffered stream, or when input is requested on a line
buffered stream that requires the transmission of
characters from the host environment. Support for these characteristics
is
implementation-defined, and may be affected via the setbuf and setvbuf
functions."

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      12-08-2005
"Michel Rouzic" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

> With the following code :
>
> int main()
> {
> int i, j, n;
>
> n=12;
> for (i=0; i<n; i++)
> {
> printf("Iteration %i out of %i\n", i+1, n);
> for (j=0; j<100000000; j++) {}
> }
> return 0;
> }
>
> With \n at the end of the line to be printed, everything will printf
> when it should
> With \r intead of \n, or just no \n or \r, nothing will get displayed
> before the loop ends (it will print it all at once, or if there's a
> \r, only the last remaining line)


As expected.


> Does anyone know the origin of this somewhat weird behavior,


There is nothing weird about it. stdio output to the terminal is
normally buffered.

> and does anyone know how to fix it so I can display the progress
> of my iterations on only one line (by using \r at the end) ?


Add

fflush(stdout);

after the printf.

Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Using fstream tellg to read a portion of the stream till the end IlyaK C++ 2 01-21-2010 02:34 AM
JavaDojo - Exceptional beta till end of May 2006 mfolb Java 12 04-03-2006 05:12 PM
Basic swap trick for stl containers!!!! (very interesting, READ till the END!) puzzlecracker C++ 6 09-20-2005 07:10 AM
290 and 291 on hold till end of April =?Utf-8?B?SmVuSA==?= MCSE 4 04-06-2005 02:26 AM



Advertisments