[c++] main function

[c++] main function

 

refer to:

"C++ from entry to proficient" People's Posts and Telecommunications Publishing House

 

    The running of the C++ program starts from the main() function. What kind of function is the main function? According to ISO C++, the main function has two forms:

        intmain()

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

    The first is the main function with no parameters.

    The second is the main function with parameters. The parameter argc-represents the number of parameters, and argv-is a string array. The filename of the Main function itself is placed in argv[0], so argc is at least 1. The parameters after the Main function are argv[1], argv[2]...

Use of main parameter:

#include<iostream>
using namespace std;

int main(int argc,char *argv[ ])
{
	
	cout<<"main参数:"<<argc<<endl;
	
	for(int i=0;i<argc;i++)
	cout<<"argv["<<i<<"]: "<<argv[i]<<endl;
	
	return 0;
}

operation result:

            


Run main.exe using the command line

    Run cmd, open the command prompt, use the cd command to switch to the folder where the program is located, enter main.exe, and the program results are as follows:

            

Run main.exe with arguments

    Use cd to switch to the folder location, enter "main.exe hello "gogo Xiao Sa" main function", the running result is as follows:

            

Note :

        1. The parameters in the main function are separated by spaces;

        2. If a parameter contains spaces, enclose it with double quotation marks "", for example: "gogo Xiao Sa".


-------------------------------------------          END       -------------------------------------

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325817961&siteId=291194637