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.
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
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
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!