main()

Program entry function

Synopsis:

int main( void );

int main( int argc, 
          const char *argv[] );

int main( int argc, 
          const char *argv[], 
          char *envp[] );

Arguments:

The arguments depend on which form of main() that you use.

argc
The number of entries in the argv array.
argv
An array of pointers to strings that contain the arguments to the program.
envp
An array of pointers to strings that define the environment for the program.

Description:

The main() function is supplied by the user and is where program execution begins. The command line to the program is broken into a sequence of tokens separated by blanks, and are passed to main() as an array of pointers to character strings in argv. The number of arguments found is passed in the parameter argc.

The argv[0] argument is a pointer to a character string containing the program name. The last element of the array pointed to by argv is NULL (argv[argc] is NULL). Arguments containing blanks can be passed to main() by enclosing them in quote characters (which are removed from that element in the argv vector). See your shell's documentation for details.

The envp argument points to an array of pointers to character strings that are the environment strings for the current process. This value is identical to the environ variable, which is defined in the <stdlib.h> header file.

Returns:

A value back to the calling program (usually the operating system).

Examples:

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char **argv )
{
    int i;
    for( i = 0; i < argc; ++i ) {
        printf( "argv[%d] = %s\n", i, argv[i] );
    }
    
    return EXIT_SUCCESS;
}

produces the output:

argv[0] = ./mypgm
argv[1] = hhhhh
argv[2] = another arg

when the program mypgm is run from the shell:

$ ./mypgm hhhhh "another arg"

Classification:

ANSI, POSIX 1003.1

Safety:  
Cancellation point No
Interrupt handler No
Signal handler No
Thread Yes