Home > Backend Development > C++ > 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
Release: 2023-09-11 18:41:02
forward
1436 people have browsed it

在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
Copy after login

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;
}

Copy after login

When you compile and run this program something like −

$ ./a.out hello people
Copy after login

output

This will give the output-

This program has 3 parameters

C:\Users\user\Desktop\hello.exe
hello
people
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template