2
Command Line Arguments in C++ It is used to give input to a program that you are going to execute. Here I will give you all example and how to use command line arguments in C++ The main function would be the following: int main (int argc, char *argv[]) CMD On *nix: $ ./my_prog arg1 arg2 CMD On Windows command line: C:\>my_prog.exe arg1 arg2

Command Line Arguments in C++

Embed Size (px)

Citation preview

Page 1: Command Line Arguments in C++

Command Line Arguments in C++

It is used to give input to a program that you are going to execute. Here I will

give you all example and how to use command line arguments in C++

The main function would be the following:

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

CMD On *nix:

$ ./my_prog arg1 arg2

CMD On Windows command line:

C:\>my_prog.exe arg1 arg2

Page 2: Command Line Arguments in C++

In both cases, given the main is declared as:

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

argc will be an int with a value of 3, argv[1] = "arg1", argv[2] = "arg2",

additionally, argv[0] will have the name of the program, my_prog.

Example of Command line arguments in C++

#include <iostream>

using namespace std;

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

{

int i;

for (i = 1; i < argc; i++)

{

cout << argv[i];

if (i < argc - 1)

cout << " ";

}

cout << endl;

return 0;

}