Introduction to main() function and command line parameters in C++

Introduction to main() function and command line parameters in C++

In C++, the main() function is the entry point of the program. It is a special function that is called first when the program starts executing and is the last function executed when the program ends. The type of the main() function is always int. According to the C++ standard, the main() function must return an integer as the exit code of the program. This exit code can be used to indicate the execution status of the program, usually 0 indicates that the program ends normally, and a non-zero value indicates that an error or abnormal situation has occurred in the program.

In C++, the main() function function declaration can become one of the following forms:

int main()

{

    // code block

    return 0; // or other integer value

}

int main() and int main(void) are equivalent, they both indicate that the main function does not accept any parameters. Both forms are legal and widely accepted in mainstream C++ compilers.

or

int main(int argc, char *argv[])

{

    // code block

    return 0; // or other integer value

}

The latter form allows us to pass arguments to the program from the command line. in:

argc is the number of command line arguments, that is, the number

argv is a pointer to an array of strings, each string represents a command line parameter, argv represents the specific content of the command line parameter, and its type is an array of character pointers. These parameters can be provided via the command line while the program is running. argv is a pointer to an array of strings, each of which stores a command-line argument. The first argument argv[0] is usually the name of the program.

Here is an example showing how to use command line arguments in C++:

#include <iostream>
using namespace std; 

int main(int argc, char* argv[]) {
    // 打印程序名称
    cout << "程序名称: " << argv[0] << endl;

    // 打印命令行参数
    for (int i = 1; i < argc; ++i) {
        cout << "参数 " << i << ": " << argv[i] << endl;
    }

    return 0;
}

If you save the program as myProgram.cpp and compile and run it, I will put the compiled file myProgram.exe in D:\cppDemo,

Arguments can be passed by entering the following in the command line cmd:

myProgram aa bb

The output is:

Program name: myProgram

Parameter 1: aa

Parameter 2: bb

See the picture below:

In this way, we can access and process these command line parameters through argc and argv in the program.

myProgram aa bb

This will cause argc to have a value of 3 (including the program name ./myProgram), and argv will be filled with the following:

char *argv[3] = {

    "./myProgram",

    "aa",

    "bb"

};

In this way, we can access and process these command line parameters through argc and argv in the program.

To sum up:

The prototype of the main() function can have two forms:

int main()

int main(int argc, char* argv[])

The first form is the most common, indicating that the main() function does not accept any parameters and does not need to process command-line parameters.

The second form indicates that the main() function accepts command-line arguments. Among them, argc represents the number of command-line parameters, argv is a pointer to an array of strings, and each string stores a command-line parameter. The first argument argv[0] is usually the name of the program.

Guess you like

Origin blog.csdn.net/cnds123/article/details/131724062