Issue
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
Also tested on few online compilers (I know they may not be a reliable option)
Tests (Compare Example 1 and Example 2)
Example 1
#include <stdio.h>
#include <unistd.h>
int main(void) {
printf("before-fork\n");
int a;
fork();
printf("%p\n", (void *)&a);
return 0;
}
OUTPUT 1
before-fork
0x7fff58cd3324
0x7fff58cd3324
Example 2 (only replaced '\n' with a space in printf("before-fork\n")
)
OUTPUT 2
before-fork 0x7ffce0196bb4
before-fork 0x7ffce0196bb4
Expected Behavior
OUTPUT 1
Is this a bug? if not then what is the reason for that?
Solution
Is this a bug?
No.
if not then what is the reason for that?
stdout
output is (by default in this case) line buffered in process local memory. The content of the buffer is flushed on newline.
Answered By - KamilCuk Answer Checked By - Clifford M. (WPSolving Volunteer)