int main()
declares a function of type
int named
main. This is, as Duoas said, the required entry point to the program. Meaning that when it is executed, it will always go to the main function first (and no others unless they are called from main), and it has to be there or it will not compile.
Being a function, you can specify what kind of arguments can be passed to it, just like any other function. These are specified in the declaration, i.e.
int main (type arg1, type arg2)
etc.
In the case of the main() function, the parameters passed to it are, by default, the number of arguments on the command line, and an array containing the name of the program as the first element, followed by any arguments given to it on the command line. These params are passed to it like they would be any other function, by the caller, which in the case of main(), is the console, and any arguments it gives are passed to main(). You can put any names for the params, such as
int main(int numberOfArgs, char* argsGiven[])
if you so choose, but most people stick to the standard arc, argv (argCount & argValue, i think, or is it argVector?). You can probably change the types as well, but you won't get the normal results.
If you want to fully understand it, read up on functions. You will learn about declaring functions, and declaring them with parameters, and passing parameters to them, as defined by the declaration.
A brief example:
C++ version
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <string>
using namespace std;
void myFunction(string str) { // a parameter defined as a string named str, is to be passed to it
cout << str << endl; // print the string that was given to the function
}
int main(int argc, char* argv[]) { // get parameters from command-line (because that's what calls the function)
cout << "My name is " << argv[0] << endl; // print program's name, first element of array
cout << "You gave me " << argc - 1 << " argument(s)." << endl; // print number of args, minus 1 for the program's name
cout << "The first one was: ";
myFunction(argv[1]); // call the function MyFunction, passing it the array element containing the first CL arg
cout << "The second one was: " << argv[2] << endl; // directly print the second arg without calling a function
return 0;
}
|
C version:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <stdio.h>
void myFunction(char* str) { // a parameter defined as a char* named str, is to be passed to it
printf("%s\n", str); // print the string that was passed to the function
}
int main(int argc, char* argv[]) { // get parameters from command-line (because that's what calls the function)
printf("My name is %s.\n", argv[0]); // print program's name, first element of array
printf("You gave me %d argument(s)\n", argc - 1); // print the number of args, minus 1 for the program's name (argc represents length of argv[])
printf("The first one was: ");
myFunction(argv[1]); // call the function MyFunction, passing it the array element containing the first CL arg
printf("The second one was: %s\n", argv[2]); // directly print the second arg
return 0;
}
|
It outputs:
C:\Bin>example arg1 arg2
My name is example.
You gave me 2 argument(s)
The first one was: arg1
The second one was: arg2
C:\Bin> |
You can compile that and run it to see how it works, if you want. If you choose to run it, make sure to give it at least one argument on the CL (command line). Otherwise it throws an exception (at least it does for me). You should try with 1 arg, then 2 args. If you only put one, it prints the second arg as "(null)" in the C version, and blank in the C++ version. Interesting eh?
Anyway, I hope that helps you understand it. If not please reply with any questions you may have.