Main function (programming)
The function main() in programming languages such as C, C++ and Java serves as the starting point of a program compiled in those languages.
It is the first user-written function run when such a program starts (some system-specific software generally runs before the main function), and usually organises the rest of the functionality of the program. The main function has access to command-line arguments that were used when the program was executed, and possible environment variables.
In C and C++, the complete main function is defined as:
int main(int argc, char **argv, char **env)
The above statement indicates that the main function returns an integer value. By default, a value of zero indicates that the program finished normally. A non-zero value indicates some sort of error, and the actual value may indicate the nature of the error.
The first argument for the main function is an integer value indicating how many command line arguments were passed, where the first argument is the name of the actual program. This allows the same executable to be called by several names, such that the program could behave differently based on its name. For some systems, only the program name appears, but on other systems the full path name is given. System-specific code must be used to extract the name of the executable, since the character used to indicate directories can be either '/' (e.g., Unix based systems) or '\' (windows based systems.)
The second argument for the main function contains a list of string pointers to the command line arguments that were passed. As mentioned above, the number of arguments passed is indicated by the first argument (traditionally called argc), and the first argument is the program name. Traditionally, command line arguments can be either options or data, where options modify the general behavior of the program and are usually preceded by some sort of flag, such as '-' (for Unix systems) or '/' (for DOS/Windoes systems), and data is most often file or directory names, but can also be things such as patterns (e.g., the grep family), etc.
Unfortunately, there is no standard for handling command line arguments. For some programs, options can be grouped together (e.g., -abc) but for others they must be separate (-a -b -c). For some programs data can directly follow options (-fname) but for others, they must be separate (-f name). Historically, options were single letters (-v), which allowed them to be grouped, but a recent trend is to use longer option names (-verbose).
The third argument for the main function is seldom used, but contains the environment variables available to the program. This consists of a series of entries of the form "varname=value" where varname is the name of the environment variable, and value is the value of that variable. This list is terminated by a NULL pointer.
For Java, the command line arguments are passed as an array of strings. Because arrays are objects in Java, there is no need for the argument count. The main function for Java is:
public static void main(String[] args)
Which indicates that the main function is an externally accessible routine that doesn't return any values.
Environment variables can be obtained using the 'System.getProperty() method.