首页 > 其他分享 >Command line arguments (argc, argv in main function)

Command line arguments (argc, argv in main function)

时间:2023-02-02 09:33:41浏览次数:51  
标签:function int argv program arguments argc out

  • argc (ARGument Count) is int and stores number of command-line arguments passed by the user including the name of the program.
    So if we pass a value to a program, value of argc would be 2 (one for argument and one for program name).
  • argv(ARGument Vector) is array of character pointers listing all the arguments.
  • Argv[0] is the name of the program.

Example:

#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
	cout << "You have entered " << argc
		<< " arguments:" << "\n";

	for (int i = 0; i < argc; ++i)
		cout << argv[i] << "\n";

	return 0;
}

/** Input:
$ g++ mainreturn.cpp -o main 
$ ./main geeks for geeks
**/

/** Output:
You have entered 4 arguments:
./main
geeks
for
geeks
**/
  • If argument itself has a space then you can pass such arguments by putting them inside double quotes "" or single quotes ''.

Example:

$ ./a.out "First Second Third"
Program Name Is: ./a.out
Number Of Arguments Passed: 2
----Following Are The Command Line Arguments Passed----
argv[0]: ./a.out
argv[1]: First Second Third

标签:function,int,argv,program,arguments,argc,out
From: https://www.cnblogs.com/shendaw/p/17084869.html

相关文章