Post by esmaeel ensanimehr
i know some work can be don on inter parameter on main function.
int main (int argc, char* argv)
argc, argv can be used in many points.
but is there any useful example for main() return value?
Return values from main() are used a lot by the invoking program
(which it can get from the operating system). The actual C code to
do that in UNIX/POSIX might be:
pid = wait(&status);
where pid is the process ID of the process that terminated, and
status is filled in with a combination of what signal the process
died on (if it was aborted) and 8 bits of the return value (if it
terminated normally). There are some cute macros to separate out
the parts of the status. There are additional complications if job
control is in use (the process may return status if it changes state
due to job control signals without actually terminating).
(or it might look at the return code from the system() function.)
Shell scripts (examples are for the Bourne shell and descendants)
can get hold of the return value in the value $?, or test success/failure
with the construct:
if command args ...
: execute here if it succeeded (exit 0)
: execute here if it failed (exit non-zero)
The "make" program will generally stop running a build if any command
it invoked returns non-zero status, although you can exempt specific
commands from this treatment so conditional code can be used in
The front-end for the compiler (if that's how the compiler is
organized) will typically stop further operations if a stage earlier
in the compilation (preprocessing, compiling, assembling, linking)
typically done as a separate process fails. Some compilers may use
a code between its components to indicate differences between
warnings (which normally don't halt compilation; -Werror on GCC is
an exception), errors (e.g. syntax errors in the program), bad
errors (e.g. can't open the input source file, or running out of
disk space writing the object file, compiler errors, and running
out of memory), and really bad errors (e.g. pieces of the compiler
There are some unofficial standards for file comparison and file
searching programs which use a 3-way status. This tends to twist
the definitions of "success" and "failure" a little. File comparison
programs include cmp, diff, rcsdiff, etc. File search programs
include grep, egrep, and fgrep. The C standard doesn't allow for
degrees of failure, and there are no required definitions for symbols
On UNIX, EXIT_SUCCESS is 0 and EXIT_FAILURE is 1.
File comparison programs:
0 - success; files are equal.
1 - success; files are NOT equal. (some people will argue this is a failure).
2 - trouble; the comparison could not be done due to nonexistent
files, file permission problems, I/O errors, or a host of other
problems. An error message should indicate the reason.
File search programs:
0 - success; search pattern found.
1 - success; search pattern NOT found. (some people will argue this is a failure).
2 - trouble; the search could not be done due to nonexistent files, file
permission problems, bad search parameters (bad regular expression),
I/O errors, or a host of other problems. An error message
should indicate the reason.
Here 1, which is the value of EXIT_FAILURE on UNIX, is used as a
lesser success code. I can think of situations where "files are
equal" is a failure in the larger view of things (e.g. the patch
failed to take), and plenty of situations where NOT finding the
string I'm looking for is not some kind of disastrous error.
There are a lot of little utility programs for use with shell
scripts, and some of them do very little BUT set the return code.
See, for example, the UNIX "test" program which can be used to
determine if a file exists, or the type of an object in the filesystem
(nonexistent, regular file, directory, socket, special device file,
FIFO, etc.) or test the relative order of file timestamps.
Mail delivery programs often follow an unofficial standard of
returning one of the codes in <sysexits.h> to indicate an error.
The error codes are oriented towards typical mail-delivery and
networking errors (e.g. host not found, host unreachable, unknown