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

M

Michel Rouzic

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) ?
 
T

Tatu Portin

Michel said:
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.
 
R

Richard Heathfield

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

Anand

Michel said:
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.
 
J

junky_fellow

Michel said:
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."
 
J

junky_fellow

Michel said:
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."
 
A

A. Sinan Unur

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
 

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
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top