printf()
buffers output until a newline is encountered. So your \n
-less version stuffs A
into the output buffer, forks.
Since both processes are identical (minus PIDs and whatnot), they now both have an output buffer that contains A
.
The execution continues and prints B
into the buffers in each process. Both processes then exit, causing a flush of the output buffer, which prints AB
twice, once for each process.
You other version, having \n
, causes that output buffer to flush after the first printf() call, and when the fork hits, both processes have an empty buffer.