Thread: Formatted output question...

  1. #1
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45

    Formatted output question...

    Why, in certain situations, does calling printf() not print the given string to the screen (stdout) until the program terminates. I ran into this with a printf() nested inside an infinite for loop; inside that for loop a child process was created, and the printf() only printed out the string when the child was terminated.

    I was told to use:

    fprintf(stderr, "string in here");

    This did in fact work, but I want a better understanding of why the normal printf() doesn't work in this situation. I can post the code if needed.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    fflush(stdout), or put a newline in the output.
    Last edited by Dave_Sinkula; 06-15-2005 at 10:13 AM. Reason: Changed/added 'or put a newline in the output'.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Lateralus
    Why, in certain situations, does calling printf() not print the given string to the screen (stdout) until the program terminates. I ran into this with a printf() nested inside an infinite for loop; inside that for loop a child process was created, and the printf() only printed out the string when the child was terminated.

    I was told to use:

    fprintf(stderr, "string in here");

    This did in fact work, but I want a better understanding of why the normal printf() doesn't work in this situation. I can post the code if needed.
    The answer has to do with buffered vs. unbuffered I/O. When you write something to stdout (via a standard printf call for example) it does not get sent immediately to the screen but rather gets sent into a buffer where it waits. When the buffer gets full, everything in the buffer then gets sent to the screen in one big chunk which is more efficient than accessing the hardware repeatedly for each individual bit of output that needs to get displayed. The buffer can also be flushed (and thus output to screen) prematurely via a call to the fflush function as already demonstrated by Dave. I think the buffer is also flushed when the program ends.

    This is the case for stdout which is one of these buffered streams. The stderr stream however is an unbuffered stream. This means that when you write a string to stderr, that string is output immediately and does not sit around in a buffer somewhere.
    Last edited by hk_mp5kpdw; 06-15-2005 at 10:45 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    Explained very well. Thanks for the quick response.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output filename / Char* question.
    By Fidicor in forum C Programming
    Replies: 3
    Last Post: 04-29-2009, 09:56 PM
  2. a output question
    By ok_good in forum C Programming
    Replies: 4
    Last Post: 10-07-2007, 03:45 AM
  3. rcp error output question
    By cristane in forum Linux Programming
    Replies: 1
    Last Post: 07-18-2005, 09:47 PM
  4. Simple question on quoting output
    By LouB in forum C++ Programming
    Replies: 13
    Last Post: 06-16-2002, 02:57 PM
  5. To all who read last post formatted output
    By spliff in forum C Programming
    Replies: 8
    Last Post: 08-21-2001, 03:37 AM