A few details of the C++ main function

  • 2020-11-18 06:23:01
  • OfStack

1. The standard prototype for the main() function

The main function is the entry function of the C++ program. The C++ standard specifies that the return value of the main() function is int. The return value is used to indicate the exit status of the program. According to the C++ standard, there are two types of main() function prototypes:


int main();

int main(int argc . char* argv[]);
// or 
int main(int argc . char** argv);

When the return value of the main() function is int and no return statement appears within the function, it can also be compiled and run normally. This is because the compiler automatically adds return 0 to the end of the main() function; Statements. Therefore, the main() function is specially processed by the C++ program. Other return value types are not void functions, and if the return statement is not used, the compiler will report an error. Although the compiler implicitly adds return 0; , but developers are advised to avoid this rule because the display does not return an error code when adding avoidable errors and does not mistake the main() function for not having AN return statement.

main() function parameters can be used to provide the user input parameters to the program, using the main() function prototype with parameters, int main(int argc,char* argv[]) , where argc represents the number of arguments, and each element in the argv array holds a string of the contents of the command-line arguments. Consider the following procedure.


#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
 if(argc>1)
 {
 cout<<"Hello "<<argv[1]<<endl; 
 }
 return 0;
}

Assuming the compiled main.out generated by this program, enter "main.out LVLV" on the console and output "Hello LVLV". Note the following when using command-line arguments.
(1) The program name entered on the command line is the first parameter of the program. argv[0] saves main.out in the above program. Although only one parameter "LVLV" is entered, the number of parameters argc contains the program name, so argc is equal to 2. In other programming languages, such as C#, command-line arguments do not contain the name of the execution file.
(2) On the command line, a space is considered to be a delimiter for a command line parameter, that is, no space is allowed inside the same parameter. If there is a space in a parameter, it can be enclosed in double quotation marks. For example, enter ES52en. out "LVLV and JF".

2. The return value of the VC++ mian() function can be of any numeric type

The return value of VC++ is not strictly required for the main() function. Any type that can be cast to int, such as char, float, double or long, can be used as the return value. Refer to the following procedure.


#include <iostream>
using namespace std;

char main()
{
 cout<<"Hello!"<<endl;
 return '0';
}

The above program can compile and run normally. Obviously, string is not the return value of the main function. If you change the return type to string, the compile will report an error and the reader can verify it. Of course, the above code is not portable, using g++ compilation under Linux environment will not pass, prompt return value type must be int, it can be seen that GNU C++ more strictly implements the content of C++ standard.

3.Windows platform can obtain the return value of main() function through the environment variable errorlevel

Respond differently depending on the return value. Write the following program.


#include <iostream>
using namespace std;
int main()
{
 int i;
 cout<<"please input a number"<<endl;
 cin>>i;
 return i;
}

This program compiles and generates ES92en. exe, and then prepares a batch file test. bat, which is as follows:

[

@echo off
main.exe
if %errorlevel%==3 echo third
if %errorlevel%==2 echo second
if %errorlevel%==1 echo first

]

When we run this batch file, type 1 from the console and get first, type 2 and get second, type 3 and get third. The operation results are as follows:

[

C:\Users\dablelv > test.bat
please input a number
2
second

]

This experiment shows that when the program ES129en.exe runs, the return value of the main() function is stored in the environment variable errorlevel, and we can use this return value to take different actions in a batch file.

In the main() function, put the statement return i ; To a function call exit(i) ; The result of this program does not change. The effect of exit(i) is to return to the operating system and return i as the result of the program. exit is used to end a process, returning the end of the process code to the operating system, and return is used to end a function call, returning the end of the function code to the caller. In the main() function, both return and exit terminate the program and return the results to the operating system. In the C language, the exit() function can be used to exit the program when an unrecoverable error occurs. But in C++ programs, the use of the exit() function breaks the program's calls to object destructors. In C++ programming, the exception handling mechanism should be used instead of calling the exit() function.

A few notes on batch files.
(1) The @ symbol appearing before the command means to close the command echo, that is, when executing the command, the console will not appear the specific content of the command, but only the execution result of the command;
(2) echo off this command is to close all command echo, plus @ symbol means to close this command echo;
(3) %a% represents a reference to the variable a.

4.main() is the first function to be executed in a program

Consider the following procedure.


#include <iostream>
using namespace std;
class A
{
public:
 A()
 {
 cout<<"In default A's constructor"<<endl;
 }
};
A b;
int main()
{
 cout<<"In main()"<<endl;
 return 0;
}

Compile and run the above code output:

[

In default A's constructor
In main()

]

In this program, the output is "In default A 's constructor", followed by "In main()". As you can see, the constructor for object a was executed before the main() function. In fact, all external object constructors are executed prior to the main() function. If member objects in a class are to be initialized, their constructors are also executed before the main() function. If other functions are called within these constructors, more can be run before the main() function. So the main() function does not have to be the first function executed in the C++ program.

These are the details of the C++ main function. For more information on C++ main function, please follow the other articles on this site!


Related articles: