The declaration of main looks like this:
int main(int argc, char *argv[]);
This indicates that main is a function returning an integer. In hosted environments such as DOS or UNIX, this value or exit status is passed back to the command line interpreter.Typically, this facility is used to direct the way the program goes about its task. It's particularly common to provide file names to a program through its arguments.
There are two arguments to main: argc and argv. The first of these is a count of the arguments supplied to the program and the second is an array of pointers to the strings which are those arguments—which are passed to the main function in the form of pointer to an array. These arguments are passed to the program by the host system's command line interpreter.
Here's an example to explain the concept:-
#include
#include
using namespace std;
int main( int argc, char* argv[] )
{
cout << "The name used to start the program: " << argv[ 0 ]<< "\nArguments are:\n";
for (int n = 1; n < argc; n++)
cout << setw( 2 ) << n << ": " << argv[ n ] << '\n';
return 0;
}
Compile it, then from the command-prompt try running it different ways:-
If the program name is "a" and it has arguments hello,world when it is run, the state of the arguments and the value of argv can be illustrated like this:
D:\prog\test> a Hello world!
The name used to start the program: a
Arguments are:
1: Hello
2: world!
Each time that argv is incremented, it is stepped one item further along the array of arguments. Thus after the first iteration of the loop, argv will point to the pointer which in turn points to the world! argument.
This is another way of giving arguments:-
D:\prog\test> cd ..
D:\prog> test\a.exe "Peter Piper" picked a peck of "pickled peppers"
The name used to start the program: test\a.exe
Arguments are:
1: Peter Piper
2: picked
3: a
4: peck
5: of
6: pickled peppers
I guess this one is self explanatory..:)
int main(int argc, char *argv[]);
This indicates that main is a function returning an integer. In hosted environments such as DOS or UNIX, this value or exit status is passed back to the command line interpreter.Typically, this facility is used to direct the way the program goes about its task. It's particularly common to provide file names to a program through its arguments.
There are two arguments to main: argc and argv. The first of these is a count of the arguments supplied to the program and the second is an array of pointers to the strings which are those arguments—which are passed to the main function in the form of pointer to an array. These arguments are passed to the program by the host system's command line interpreter.
Here's an example to explain the concept:-
#include
#include
using namespace std;
int main( int argc, char* argv[] )
{
cout << "The name used to start the program: " << argv[ 0 ]<< "\nArguments are:\n";
The name used to start the program: a
Arguments are:
1: Hello
2: world!
Each time that argv is incremented, it is stepped one item further along the array of arguments. Thus after the first iteration of the loop, argv will point to the pointer which in turn points to the world! argument.
This is another way of giving arguments:-
D:\prog\test> cd ..
D:\prog> test\a.exe "Peter Piper" picked a peck of "pickled peppers"
The name used to start the program: test\a.exe
Arguments are:
1: Peter Piper
2: picked
3: a
4: peck
5: of
6: pickled peppers
I guess this one is self explanatory..:)
No comments:
Post a Comment
Type in for your comments and queries...