fork() in c using printf [duplicate]

You’re running into the buffering behaviour of your system’s printf implementation. In the first case, the string is printed to a buffer, but since there’s no newline (and you didn’t call fflush), it’s just sitting there in that buffer. Then you fork, and both forked buffers are flushed when their respective processes exit.

In the second case, the \n causes the buffer to be flushed before the fork, so there’s no output remaining when the forked processes exit.

Leave a Comment