# Stdout and C code

**URL:** <https://discourse.julialang.org/t/stdout-and-c-code/13533>\
**Category:** General Usage\
**Created:** [August 15, 2018, 11:26pm UTC](https://discourse.julialang.org/t/stdout-and-c-code/13533 "2018-08-15T23:26:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [August 15, 2018, 11:26pm UTC](https://discourse.julialang.org/t/stdout-and-c-code/13533/1 "2018-08-15T23:26:20Z")

</div>

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
$ 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?

---

<div class="post-metadata">

**Author:** ![ssfrr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ssfrr/32/3736_2.png) [@ssfrr](https://discourse.julialang.org/u/ssfrr)\
**Post date:** [August 16, 2018, 12:32am UTC](https://discourse.julialang.org/t/stdout-and-c-code/13533/2 "2018-08-16T00:32:30Z")

</div>

ah, from [issue #8765](https://github.com/JuliaLang/julia/issues/8765) I see that adding `flush_cstdio()` will flush the C stdio to the output, so this works as expected:

```julia
$ 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?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 16, 2018, 7:15am UTC](https://discourse.julialang.org/t/stdout-and-c-code/13533/3 "2018-08-16T07:15:22Z")

</div>

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.
