21

printf function in c doesn't always print the output on screen. For example if you forget to put \n at the end of string you are printfing you sometimes don't get the o/p. Are there some other conditions when printf doesn't print. I remember someone saying that there are 7 such conditions. Can you guys please help.

disputedbug
  • 265
  • 1
  • 3
  • 6
  • 6
    Are you sure you're thinking of `printf`, and not the preconditions of the apocalypse? – Kerrek SB Feb 16 '12 at 19:13
  • 3
    @KerrekSB Or maybe the signs of an impending homework with 7 items ? – cnicutar Feb 16 '12 at 19:13
  • 1
    This seems platform specific, dependent on output device. What OS and system are you using? – Ben Zotto Feb 16 '12 at 19:13
  • 4
    Well, if the monitor is unplugged, if a meteor crashes into your computer, if a rabid weasel claws at the screen.... –  Feb 16 '12 at 19:14
  • 2
    http://stackoverflow.com/q/1716296/469210 – borrible Feb 16 '12 at 19:14
  • It may not print if you provide invalid format specifiers, or any other forms of undefined behaviour relating to `printf`. – dreamlax Feb 16 '12 at 19:15
  • 1
    The bit about the newline at the end is real enough, it happens because `stdout` is usually line-buffered... But 7 reasons? Why 7? – cha0site Feb 16 '12 at 19:16
  • 4
    @JackManey, on some platforms, it will still print even with the rabid weasel. This problem is not cross-platform. – Jonathan M Feb 16 '12 at 19:16
  • This smells like an upcoming test review question to me. – Rabbit Feb 16 '12 at 22:43
  • 3
    If an interrupt makes sufficient high-priority rabid weasels ready, and they insist on chasing their tails, the printf() may never complete. – Martin James Feb 17 '12 at 14:22
  • 3
    printf() may be directed to attempt to print an unterminated weasel. – Martin James Feb 17 '12 at 14:25
  • 3
    printf() may make a function call to return a parameter. If this fails, an Eweasel exception may be raised. – Martin James Feb 17 '12 at 14:26
  • 2
    Windows update may restart the box before the printf() can complete. – Martin James Feb 17 '12 at 14:28
  • 2
    STDOUT may have been redirected, the network cable has fell out and the printf() blocks forever. – Martin James Feb 17 '12 at 14:31
  • Possible duplicate of [Why does printf not flush after the call unless a newline is in the format string?](https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) – phuclv Mar 22 '19 at 15:24

3 Answers3

28

Standard out is a buffered stream, it is not guaranteed to flush unless a newline is put in, the stream is closed, or the program exits normally. If the program exits abnormally, it is possible for the stream to not flush. Standard out is line buffered, which is why a newline will flush it. There are buffers that will not flush with a newline.

user1214634
  • 396
  • 3
  • 3
  • I know that this is a very old question but what kind of thing can inhibit a `printf` of printing ? – Mouin Oct 24 '17 at 12:39
10

its not that printf won't always print, its that it isn't guaranteed to print immediately. This means that if you are using it for debugging purposes, then you can't guarantee that it will happen exactly when it does in the code. If you want to make sure that it does print exactly when you said it call fflush(stdout).

Note: You typically don't want to use fflush(stdout) unless you are debugging, its really resource intensive and if you care about speed performance at all it has the potential to slow you down.

ideasman42
  • 42,413
  • 44
  • 197
  • 320
Irony
  • 317
  • 1
  • 3
  • 11
0

I used

puts(largeString); 

because in my particular case, printf() just stopped printing halfway through. The entire string was there, it just didn't print.

fflush(stdout) didn't fix it either, another printf() on the next line printed just fine.

Jacob Bruinsma
  • 1,087
  • 1
  • 10
  • 23