How can two condition works in if-ifelse-else statement?

c, if-statement

Solution

No, you forked it. There are two processes running. One reported the ls and the other reported the printf().

Specifically, the child/forked process executed /bin/ls and the parent called printf(), the output you see.

Problem

My professor shows the following example in C: ``` #include <sys/types.h> #include <stdio.h> #include <unistd.h> int main() { pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); return 1; } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child */ wait (NULL); printf("Child Completed its execution\n"); } return 0; } ``` I compiled it and ran it. I saw a strange behavior in this code: The result of 'ls' program/command is printed which is in the else if condition but also the string "Child Completed its execution\n" printed too which is in else. Isn't this a strange behavior?

Original source