In addition to what has already been said about the return code, I can add that it is correct (meaningful for the environment) to complete the program in 4 ways.
This is the return statement already considered, the exit () and raise () functions, and the _exit () system call.
The actions for return and exit () call are the same. For details on exit (), _exit () and raise (), see the corresponding man.
This can be illustrated with such a program:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> void fin() { puts("atexit func"); } int main (int ac, char *av[]) { atexit(fin); if (!av[1]) _exit(puts("_exit") == EOF); else if (*av[1] == 'x') exit (puts("exit") == EOF); else if (*av[1] == 'r') raise(SIGTERM); return (puts("return") == EOF); }
But its result
avp@avp-xub11:~/hashcode$ for i in x 1 '' r; do > echo ./a.out $i; ./a.out $i; echo rc = $?; done ./a.out x exit atexit func rc = 0 ./a.out 1 return atexit func rc = 0 ./a.out _exit rc = 0 ./a.out r Terminated rc = 143 avp@avp-xub11:~/hashcode$
The output rc = ... is the same return code with which the program "talks" about how it ended. Traditionally, 0 is successful.
By the way, in windows return codes are also used. For example (portable for Makefile (!))
myprog && echo Success
on the command line will display Success
only if myprog
successfully completed.
void
should always return a value! - mega Nov.void
is a type specifier, means "nothing" and there are no procedures in the boxes. Here, only functions, and a function that returns void, can also return a value — the samevoid
: void a () {} void b () {return a (); } - mega Nov.