main(3C)
_________________________________________________________________
main function
Enter a C main program.
_________________________________________________________________
Calling Sequence
main ([argc, argv, envp])
int argc;
char *argv[];
char *envp[];
{
}
where argc is optional and is the number of arguments,
including the program name, with which you invoke
the C program.
argv is optional and is a pointer to an array of
pointers to strings; argv[0] is the name you invoked
the program with; argv[1] is the first argument on
the command line after the program name; argv[argc]
is a null pointer.
envp is optional and is a pointer to an array of
pointers to strings, each of which is a separate
environment variable of the form NAME = VALUE. The
getenv function (described earlier in this chapter)
accesses this list of environment variables. The
UNIX routines execle and execve (described in
Chapter 2 of Using Specialized C Functions) can
change the environment of the new process if it is a
C program.
Description
Every C program has at least one function: the main function.
This function provides a place for the program to begin
execution; the main function must be present to initiate a C
program. The runtime initializer calls main and returns to the
system when main returns.
Since argv[0] is the program name, the initial value of argc is
always at least 1. If you want to manipulate the argv character
arrays as something other than strings, you must make explicit
use of such functions as atof or atoi.
If your program does not process command line arguments, begin
the function with the following:
DG/UX 4.00 Page 1
Licensed material--property of copyright holder(s)
main(3C)
main()
Returns
The main function's return value is used as an argument to the
exit function. The value 0 typically means no errors occurred,
and a non-zero value indicates an error condition. The wait
function can retrieve this value.
Related Functions
See also the getenv function, described in this chapter, and the
exec+, exit, and wait functions, described in Chapter 2 of Using
Specialized C Functions.
Example
/* Program test for the main() function */
int i = 1;
main(argc, argv, envp)
int argc;
char *argv[];
char *envp[];
{
printf("You called the program %s.\n", argv[0]);
while (i < argc) {
printf("Argument %d for this run is %s.\n",
i, argv[i]);
i++;
}
}
A call to the program test with arguments alpha, beta, and gamma
generates the output
You called the program test.
Argument 1 for this run is alpha.
Argument 2 for this run is beta.
Argument 3 for this run is gamma.
DG/UX 4.00 Page 2
Licensed material--property of copyright holder(s)