arguments in the main()

int main(int argc, char *argv[])


what these arguments......
argc is the number of arguments passed to your program
argv is an array of C strings with the value of the arguments passed to your program
argv[0] is always the name of your program.
who passes these arguments....and how to use them..........
The user via command line, or by launching your program from a file by "Open With"

You can use it this way:
1
2
3
4
5
6
7
8
9
10
//Program: foobar
#icnlude <string>
#include <iostream>
int main ( int argc, char *argv[] )
{
   if ( argc != 2 )
       cout << "Usage: foobar name";
   else
      cout << "Hello " << argv[1] << "!!!!";
}
Command line:
foobar Bazzy
Output:
Hello Bazzy!!!!
what happens when the program is launched with "open with ".......is it the operating system then.......
argv[1] == filepath/filename
You can use it to open the file
You make command-line options like
foobar arg1 arg2
But the arguments/parameters are not passed to main() directly. Instead, two parameters are passed, namely int argc and char *argv[].

argc (argument count) is an integer counting the number of arguments on the command line, including the name of the program itself. The one shown above has 3.

argv (argument vector) is a an array of pointers to character strings. In the case above, argv[0] will be the name foobar, argv[1] will be the first parameter, represented as a string.
Topic archived. No new replies allowed.