another qustion about fork

hi there this program runs
but i m jus trying out a few things here to have an indepth knowledge about different possibilities.
can someone comment on this please.

#include<stdio.h>
#define DIM 8
int main()
{
int pid, i, ans;
int arr[DIM] = {1,2,3,4,5,6,7,8};

    pid = fork\(\);
    /*printf\("%d\\n",pid\);
    printf\("%d\\n",getpid\(\)\);
    printf\("%d\\n",getppid\(\)\);
    */
    
    if\(pid==0\)
    \{
            ans = 0;
            for\(i=0;i&lt;DIM;i\+\+\)
            \{
                    ans = ans \+ arr[i];
                    printf\("Child sum %d\\n", ans\);
            \}
            _exit\(0\);
    \}

// printf("%d\n",getpid());
if(pid<0)
{
fprintf(stderr, "error\n");
exit(1);
}

    ans = 1;
    for\(i=0;i&lt;DIM;i\+\+\)
    \{
            ans = ans * arr[i];
            printf\("Parent %d\\n",ans\);
    \}

}

heres my out put

Child sum 1
Child sum 3
Child sum 6
Child sum 10
Child sum 15
Child sum 21
Child sum 28
Child sum 36
Parent 1
Parent 2
Parent 6
Parent 24
Parent 120
Parent 720
Parent 5040
Parent 40320

=========
question: what if i had an if loop sayin
if(pid>0)
{
ans = 1;
for(i=0;i<DIM;i++)
{
ans = ans * arr[i];
printf("Parent %d\n",ans);
}
}

the only difference here is i put a condition. but if i don't have a condition what would happen?
is there any difference?
does it matter?

pid can be less than zero, equal to zero, or greater than zero. You already tested for equal to zero. And you tested for less than zero.

That means that pid must be greater than zero when it reaches that code. Putting in an explicit test will not change the way the program runs. And it will slow it down, but not measurably so.

Still I would put the explicit test in. This makes the code more readable. It also makes "paragraph 3" more self sufficient. These things make future changes to the program easier.

perderabo,
thanks for the answer. i was just makin sure :smiley:
Cheers!