In a Java application, execution begins in a static
method (void
main(String[])
) of a specified class. In C,
execution also begins at a function called main
, but it has the
following prototype:
int main(int argc, char **argv);
The parameters represent an array of character strings
that form the command that ran the program. argv[0]
is usually the name of the
program, argv[1]
is the first
argument, argv[2]
is the
second, …, argv[argc - 1]
is the last, and
argv[argc]
is a null pointer.
For example, the command:
myprog wibbly wobbly
…may cause main
to be invoked as if
by:
char a1[] = "myprog"; char a2[] = "wibbly"; char a3[] = "wobbly"; char *argv[4] = { a1, a2, a3, NULL }; main(3, argv);
The parameters are optional (you can replace them with
a single
void
), but main
always returns
int
in any portable program. Returning 0
tells the environment that the program
completed successfully. Other values
(implementation-defined) indicate some sort of failure.
<stdlib.h>
defines
the macros EXIT_
and
EXIT_
as
symbolic return codes.