Every C program coded to run in a hosted execution environment contains the definition (not the prototype) of a function called
main, which is the designated start of the program.
|/* another implementation-defined signature */||(4)|
|argc||-||Non-negative value representing the number of arguments passed to the program from the environment in which the program is run.|
|argv||-||Pointer to an array of pointers to null-terminated multibyte strings that represent the arguments passed to the program from the execution environment (argv through argv[argc-1]). The value of argv[argc] is guaranteed to be 0.|
|body||-||The body of the main function|
|other_parameters||-|| Implementations may allow additional forms of the main function. A very common extension is passing a third argument of type |
argv are arbitrary, as well as the representation of the types of the parameters: int main(int ac, char** av) is equally valid.
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.
main 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 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
argv .. argv[argc-1] point at the first characters in each of these strings.
argv is the pointer to the initial character of a null-terminated multibyte strings that represents the name used to invoke the program itself (or, if this is not supported by the execution environment, argv is guaranteed to be zero).
If the execution environment cannot distinguish between uppercase and lowercase letters, the command line arguments are converted to lower case.
The strings are modifiable, and any modifications made persist until program termination, although these modifications do not propagate back to the execution environment: they can be used, for example, with strtok.
The size of the array pointed to by
argv is at least
argc+1, and the last element,
argv[argc], is guaranteed to be a null pointer.
main function has several special properties:
mainwithout encountering a return statement, the effect is that of executing return 0;.
Demonstrates how to inform a program about where to find its input and where to write its results.
Invocation: ./a.out indatafile outdatafile
argc = 3 argv --> ./a.out argv --> indatafile argv --> outdatafile argv[argc] = (null)
- C11 standard (ISO/IEC 9899:2011):
- 22.214.171.124.1 Program startup (p: 13)
- C99 standard (ISO/IEC 9899:1999):
- 126.96.36.199.1 Program startup (p: 12)
- C89/C90 standard (ISO/IEC 9899:1990):
- 188.8.131.52 Hosted environment
C++ documentation for Main function