B.2 Environment

Arguments to main

The semantics of the arguments to main (§2.1.2.2.1)

In Microsoft C, the function called at program startup is called main. There is no prototype declared for main, and it can be defined with zero, two, or three parameters:

int main( void )

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

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

The third line above, where main accepts three parameters, is a Microsoft extension to the ANSI standard. The third parameter, envp, is an array of pointers to environment variables. The envp array is terminated by a null pointer. See “The main Function and Program Execution” for more information about main and envp.

The variable argc never holds a negative value.

The array of strings ends with argv[argc], which contains a null pointer.

All elements of the argv array are pointers to strings.

A program invoked with no command-line arguments will receive a value of one for argc, as the name of the executable file is placed in argv[0]. (In DOS versions prior to 3.0, the executable-file name is not available. The letter “C” is placed in argv[0].) Strings pointed to by argv[1] through argv[argc – 1] represent program parameters.

The parameters argc and argv are modifiable and retain their last-stored values between program startup and program termination.