Q: Parent code is called twice in multi process program in C I have been learning about fork in c and i have this program:
 int main(void) {
     int i;
     printf("Start program\n");
     printf("This is parent process %d: %d\n", getpid(), i);
     int pid = fork();
     printf("%d ", pid);
     if(pid == 0) {
       printf("This is process %d: %d\n", getpid(), i);
     } 
   return 0;
 }

This is the output:
Start program
This is parent process 4467: 0
4578 Start program
This is parent process 4467: 0
0 This is process 4578: 0

I do not understand why the parent code is called twice.

A: stdout is buffered. See e.g. setvbuf(3).
You forgot to call fflush(3) before doing your fork(2). As a rule of thumb, you'll better do fflush(NULL) before any fork().
That could explain the observed behavior (because flushing happens later in both parent and child processes, e.g. at program exit or return from main in crt0). You might (on Linux) use strace(1) to understand more exactly what is happenning.
BTW,  fork(2) could fail. You should handle that (that is, handle the pid == -1  (or pid<0) case...).
