By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

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

P: n/a
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) ?

Dec 8 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Dec 8 '05 #2

P: n/a
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)
Dec 8 '05 #3

P: n/a
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
Dec 8 '05 #4

P: n/a

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

Dec 8 '05 #5

P: n/a

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

Dec 8 '05 #6

P: n/a
"Michel Rouzic" <Mi********@yahoo.fr> wrote in
news:11**********************@g43g2000cwa.googlegr 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 <1u**@llenroc.ude.invalid>
(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
Dec 8 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.