I am pretty new to unix programming and came across something I do not understand.
Here it is the following code snippet:
#include <stdio.h>
#include <sys/types.h>
int main()
{
printf("%d ", fork());
return 0;
}
The output is: 9298 0.
Why does the child process call this printf ? At my course I was told that it executes everything after the fork() call. What am I getting wrong ?
>Solution :
The result of fork is passed to printf so it is executed before printf. Your code is equivalent to:
#include <stdio.h>
#include <sys/types.h>
int main()
{
pid_t pid = fork();
printf("%d ", pid);
return 0;
}