I use the functions fork(),exec()...
But how can this program be compiled without including some extra headers (like sys/types.h, sys/wait.h).
I use ubuntu 1
In classic "ANSI" C, if you call a function without declaring it the compiler behaves as if the function was implicitly declared to take a fixed-but-unspecified number of arguments and return int
. So your code acts as if fork()
and execvp()
were declared thus:
int fork();
int execvp();
Since execvp()
takes a fixed number of arguments and returns int
, this declaration is compatible. fork()
also takes a fixed number of arguments, but returns pid_t
; however since pid_t
and int
are equivalent types on most Linux architectures, this declaration is effectively compatible too.
The actual definitions of these functions are in the C standard library, which is linked by default, so the definition is available at link time and thus the code works.
As Keith Thompson notes, this language feature was dropped in the C99 revision of the C language standard, and compilers invoked in C99 or C11 mode must at least issue a warning when a function is called without being explicitly declared.