What is this doing?

Well I am doing some tuts and I am been reading some revised code of the examples.

Now From my other programming experiances the main is calling for some arguement and the cpp file calls 2 libraries.

What I am wondering what is this line of code doing exactly

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;
int main(int nNumberofArgs, char* pszArgs[])

What are the 2 Classes or libraires its calling to and,what is the arguement in the brackets? under the int main.

It would help me alot if you could break it done for me a bit.
I know HTML and VB so I do have some exp. with coding.

I also have severel books I am working from crawling through em one page at a time.

The book doesnt explain what the arguement does. very well and google was no help.

http://infamouskiller.wordpress.com/
Last edited on
It is usually expressed as int main( int argc, char* argv[] )
where the postscript 'c' means 'count' and 'v' means 'variable-length'.

When you execute a program from the command-line, you type its name followed by zero or more argument strings. For example:
(Windows) dir /b *.txt
(Unix) ls -CF foo.txt

In both of these examples, the program is executed with two arguments --which are themselves character strings:
'dir' has arguments "/b" and "*.txt"
'ls' has arguments "-CF" and "foo.txt"

The trick is that the actual number of arguments the user will type is unknown. So the main() function in a C or C++ program simply gives you an array of char* and the number of elements in that array. (It also treats the program name as the first argument.)

To play with it, compile and play with the following program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iomanip>
#include <iostream>
using namespace std;

int main( int argc, char* argv[] )
  {
  cout << "Program \"" << argv[ 0 ] << "\" was run with " << (argc -1) << " arguments:\n";

  for (int n = 1; n < argc; n++)
    {
    cout << setw( 2 ) << n << " \"" << argv[ n ] << "\"\n";
    }

  return 0;
  }


If I were to compile it to a.exe, I could try:

a.exe
a.exe hello world
a.exe x - y + z
a.exe 42
a.exe "hello world"

Hope this helps.
Topic archived. No new replies allowed.