The main function of C++ displays the parameters passed in by the operating system (Win11, VS2022)

Create a new empty project in VS2022, add the file "main.cpp", and enter the following code:

#include <iostream>
using namespace std;
int main(int argc, char* argv[], char* envp[])
{
	//	显示命令行参数.	
	cout << "命令行参数: " << endl;
	for (int i = 0; argv[i] != 0; i++) {
		cout << argv[i] << endl;
	}
	//	显示当前程序运行环境的参数.
	cout << "当前程序运行环境的参数: " << endl;
	for (int i = 0; envp[i] != 0; i++) {
		cout << envp[i] << endl;
	}
	return 0;
}

Assuming that the compiled file is named mytest.exe, copy the file to the directory "d:\temp", enter the directory d:\temp on the command line, and execute the command "mytest abc", the running result is shown in the figure below.

The result of running the program shows the parameters of the command line and the running environment of the program. 

Guess you like

Origin blog.csdn.net/Alexabc3000/article/details/128510452