Home  >  Article  >  Backend Development  >  In C/C++, int argc and char *argv are used to receive command line parameters. Among them, int argc represents the number of command line parameters, and char *argv is an array of pointers used to store the string of each command line parameter.

In C/C++, int argc and char *argv are used to receive command line parameters. Among them, int argc represents the number of command line parameters, and char *argv is an array of pointers used to store the string of each command line parameter.

PHPz
PHPzforward
2023-09-11 18:41:021378browse

在C/C++中,int argc和char *argv是用来接收命令行参数的。其中,int argc表示命令行参数的数量,而char *argv是一个指针数组,用来存储每个命令行参数的字符串

argc represents the parameter count and argv represents the parameter value. These are the variables passed to the main function when it starts executing. When we run a program, we can provide parameters to the program, such as −

$ ./a.out hello

Example

where hello is a parameter of the executable file. You can access it in your program. For example,

#include<iostream>
using namespace std;
int main(int argc, char** argv) {
   cout << "This program has " << argc << " arguments:" << endl;
   for (int i = 0; i < argc; ++i) {
      cout << argv[i] << endl;
   }
   return 0;
}

When you compile and run this program something like −

$ ./a.out hello people

output

This will give the output-

This program has 3 parameters

C:\Users\user\Desktop\hello.exe
hello
people

Please note that the first parameter is always the location where the executable file is executed.

The above is the detailed content of In C/C++, int argc and char *argv are used to receive command line parameters. Among them, int argc represents the number of command line parameters, and char *argv is an array of pointers used to store the string of each command line parameter.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Storage classes in CNext article:Storage classes in C