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

Discussion in 'C Programming' started by Michel Rouzic, Dec 8, 2005.

  1. 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) ?
    Michel Rouzic, Dec 8, 2005
    #1
    1. Advertising

  2. Michel Rouzic

    Tatu Portin Guest

    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
    Tatu Portin, Dec 8, 2005
    #2
    1. Advertising

  3. 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)
    Richard Heathfield, Dec 8, 2005
    #3
  4. Michel Rouzic

    Anand Guest

    Re: printf() in a loop without \n won't display anything till theend

    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
    Anand, Dec 8, 2005
    #4
  5. Michel Rouzic

    Guest

    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, 2005
    #5
  6. Michel Rouzic

    Guest

    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, 2005
    #6
  7. "Michel Rouzic" <> wrote in
    news::

    > 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Dec 8, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mfolb
    Replies:
    12
    Views:
    652
    Oliver Wong
    Apr 3, 2006
  2. ben
    Replies:
    4
    Views:
    603
    Martin Ambuhl
    Jun 26, 2004
  3. puzzlecracker
    Replies:
    6
    Views:
    2,596
    John Harrison
    Sep 20, 2005
  4. IlyaK
    Replies:
    2
    Views:
    1,420
    IlyaK
    Jan 21, 2010
  5. Isaac Won
    Replies:
    9
    Views:
    354
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page