C++ argv: Command-Line Arguments Explained
In C++, 'argv' is a parameter of the 'main' function that represents the command-line arguments passed to the program when it is executed. It is an array of C-style strings ('char*'), where each element represents an argument. The first element ('argv[0]') is always the name of the program itself. The remaining elements ('argv[1]', 'argv[2]', etc.) are the actual arguments provided by the user.
Here's an example of how 'argv' can be used in a C++ program:
#include <iostream>
int main(int argc, char* argv[]) {
// argc represents the number of command-line arguments
// argv is an array of C-style strings representing the arguments
std::cout << "Number of arguments: " << argc << std::endl;
std::cout << "Arguments:" << std::endl;
for (int i = 0; i < argc; i++) {
std::cout << "Argument " << i << ": " << argv[i] << std::endl;
}
return 0;
}
When you run this program with command-line arguments, it will print the number of arguments and the value of each argument. For example, if you run the program as ./program arg1 arg2, the output will be:
Number of arguments: 3
Arguments:
Argument 0: ./program
Argument 1: arg1
Argument 2: arg2
Note that the 'argc' parameter represents the number of arguments, including the program name itself ('argv[0]').
原文地址: https://www.cveoy.top/t/topic/o2Pw 著作权归作者所有。请勿转载和采集!