c/language/main function

Every C program coded to run in a hosted execution environment contains the definition (not the prototype) of a function named, which is the designated start of the program.

Parameters
The names and  stand for "argument count" and "argument vector", and are traditionally used, but other names may be chosen for the parameters, as well as different but equivalent declarations of their type:  is equally valid.

A common implementation-defined form of main is, where a third argument, of type , pointing at an array of pointers to the execution environment variables, is added.

Return value
If the return statement is used, the return value is used as the argument to the implicit call to exit (see below for details). The values zero and exit_success indicate successful termination, the value exit_failure indicates unsuccessful termination.

Explanation
The function is called at program startup, after all objects with static storage duration are initialized. It is the designated entry point to a program that is executed in a hosted environment (that is, with an operating system). The name and type of the entry point to any freestanding program (boot loaders, OS kernels, etc) are implementation-defined.

The parameters of the two-parameter form of the main function allow arbitrary multibyte character strings to be passed from the execution environment (these are typically known as command line arguments). The pointers point at the first characters in each of these strings. (if non-null) is the pointer to the initial character of a null-terminated multibyte string that represents the name used to invoke the program itself (or, if this is not supported by the host environment, is guaranteed to be zero).

If the host environment cannot supply both lowercase and uppercase letters, the command line arguments are converted to lowercase.

The strings are modifiable, and any modifications made persist until program termination, although these modifications do not propagate back to the host environment: they can be used, for example, with strtok.

The size of the array pointed to by is at least, and the last element, , is guaranteed to be a null pointer.

The function has several special properties: @1@ A prototype for this function cannot be supplied by the program. @2@ If the return type of the main function is with, then the return from the initial call to main (but not the return from any subsequent, recursive, call) is equivalent to executing the exit function, with the value that the main function is returning passed as the argument (which then calls the functions registered with atexit, flushes and closes all streams, and deletes the files created with tmpfile, and returns control to the execution environment). @3@{{rrev multi|until1=c99|rev1= If the main function executes a that specifies no value or, which is the same, reaches the terminating } without executing a, the termination status returned to the host environment is undefined. }}
 * rev2=If the return type of the main function is not with  (e.g. ), the value returned to the host environment is unspecified. If the return type is compatible with  and control reaches the terminating }, the value returned to the environment is the same as if executing.