Stdout and C code

It seems that the stdout that most of Julia prints to is not the same as the stdout that C’s printf will print to, for example:

$ julia -e 'println("before"); ccall(:printf, Cint, (Ptr{Cchar},), "hello\n"); println("after")'
before
after
hello

I came across this while debugging another issue (not sure if it’s related) and putting some printfs in my C code, and realizing that none of the C prints showed up until after the julia process exits.

  1. can someone give a quick run-down of the relationship between Julia’s stdout and what printf prints to?
  2. Is there a way to get a file descriptor for Julia’s stdout that I can pass into my C code to add fprintf statements that will be visible in the normal Julia output stream?

ah, from issue #8765 I see that adding flush_cstdio() will flush the C stdio to the output, so this works as expected:

$ julia7 -e 'println("before"); ccall(:printf, Cint, (Ptr{Cchar},), "hello\n"); Base.Libc.flush_cstdio(); println("after")'
before
hello
after

So I guess the C prints go to some intermediate stream that is buffered separately and needs to be flushed to the main stdout stream?

2 Likes

As far as I remember, standard STDOUT on linux is buffered by default. If that’s what’s biting you, try printing to STDERR in C instead - i think that one is not buffered by default.