1

I've added some printf statements to some code that is run at the start of a program, but the statements aren't printing to stdout until I terminate the program with a ctrl c in my terminal, and they are showing up on the same line as my terminal prompt. For example the printf in the demo code below would show like this

[notice] Interrupt: exiting.
yabbadabbadoo/usr/local/bin 13:24:21 : 

Why is that and how to change it? i.e. would there be somewhere in the application code that the developer has somehow set printf statements to print at exit?

some_function_name(){
  printf("yabbadabbadoo");

}
Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234
Leahcim
  • 35,491
  • 52
  • 174
  • 313

1 Answers1

2

would there be somewhere in the application code that the developer has somehow set printf statements to print at exit?

No, in general, the standard output stdout is line buffered. So unless the buffer is either full, or received a newline, the content is not flushed.

While the program is terminating, all open buffers are flushed automatically, that is why you get to see the output after terminating the program.

In your case, you need to force the buffer to be flushed to make the output appear on the terminal. You can either

  • add a newline '\n' at the end of format string.
  • use fflush(stdout) after the printf() call

to ensure the flushing.

Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234