The declaration of the main() function of a program that allows command-line arguments to be passed. The following pointers may help, argc must be >= 1, as the name of the program itself counts as one argument, the first index of *argv is the name of the program. By using this, one can write programs to accept arguments from the command-line in Unix-like operating systems, or DOS.

In fact, it should be int main(int argc, char **argv), int main(int argc, char *argv[]), or int main(int argc, char argv[][]). argv is an array of strings, which in turn are arrays of bytes. Also, according to the ANSI C standard, main does not have to return void, but ANSI C++ says it does. (Don't you just love consistancy in standards?]) I might also had that (traditionally) argc is an abriviation for "argument count", and argv is an abriviation for "argument values", although you can call those variables anything you want as long as there are two of them, and the first is an int, and the second is char **. (Therefore int main(int foo, char **bar) would also be an appropriate title.)

The main() function is special in C and C++ programs: it provides the means for other programs (such as a shell) to call the executable program. Therefore, when you link a bunch of object files together (*.o files) into an executable, exactly one of them must define a main() function.

The function type of int(int,char *[]) matches the convention that when the resulting function is called, its arguments will be passed as an array of C strings in the second argument, the number of arguments in the first argument, and its return value will be passed to the environment as an integer return code, where the value of is interpreted as success and anything else as the indication of some error. So even if your C compiler accepts a different declaration for this function, it will still be called in this way by the calling environment, unless you wrote that environment yourself and made it use a nonstandard calling convention, a practice that would earn you the eternal scorn and contempt of every C programmer on earth.

For those interested in the Windows API, for a non-console program, the entry point is WinMain. (Win32 Console programs still get the main() prototyping scheme to determine their entry point).

WinMain's prototype looks something like this:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

Now the third parameter (LPSTR is really just Hungarian Notation for char*) is where you get your command line as a string (you do not get the free tokenization here). The problem here is that the string that you receive is single-byte (the same as you would in argv, argc), and oftentimes you will need a Unicode string (for localized apps, etc).

To get the Unicode command line, you could have to call:
LPWSTR wargv = CommandLineToArgvW(GetCommandLine(), *nArgs);

This will get the Unicode command line as a TCHAR string, and dump it into CommandLineToArgvW to get a similar result to the standard argv, argc combination. According to the C/C++ standard, command-line strings (argv) are not Unicode, and this is the solution Microsoft put forth. The new wargv string is a double-byte character string, and is suitable for all versions of an application. (Command lines are still very important to Win32 executables, as they tell the program in what mode to run in, as determined by the individual app).

Log in or registerto write something here or to contact authors.